package com.uzai.console.service.sys.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.taobao.api.internal.toplink.embedded.websocket.util.StringUtil;
import com.uzai.common.enums.DeviceTypeEnum;
import com.uzai.common.enums.DeviceTypeGroupEnum;
import com.uzai.common.enums.RedisCacheKeyEnum;
import com.uzai.common.exception.BusinessException;
import com.uzai.common.result.ResponseCode;
import com.uzai.common.utils.CommonUtil;
import com.uzai.console.common.ResponseMsg;
import com.uzai.console.common.utils.DateUtil;
import com.uzai.console.common.utils.DesensitizeUtil;
import com.uzai.console.common.utils.RandomUtil;
import com.uzai.console.common.utils.Tools;
import com.uzai.console.config.UzaiConsoleSysConfig;
import com.uzai.console.dto.faq.privatework.FaqKeywordQuery;
import com.uzai.console.dto.faq.privatework.FaqReplyQuery;
import com.uzai.console.dto.profit.inviterewardrule.InviteRewardRuleQuery;
import com.uzai.console.dto.profit.ladderratio.LadderRatioQuery;
import com.uzai.console.dto.profit.ladderratiogroup.LadderRatioGroupQuery;
import com.uzai.console.dto.profit.normalrewardrule.NormalRewardRuleQuery;
import com.uzai.console.dto.profit.nuserrewardrule.NuserRewardRuleQuery;
import com.uzai.console.dto.profit.nuserrewardrulegroup.NuserRewardRuleGroupQuery;
import com.uzai.console.dto.profit.prirewardrule.PriRewardRuleQuery;
import com.uzai.console.dto.profit.regrewardrule.RegRewardRuleQuery;
import com.uzai.console.dto.sys.copyconfigplan.CopyConfigPlanSaveDto;
import com.uzai.console.dto.sys.copyconfigplan.CopyPrivateConfigPlanDto;
import com.uzai.console.dto.takebal.redpackettemplate.RedPacketTemplateQuery;
import com.uzai.console.dto.user.usertype.UserTypeQuery;
import com.uzai.console.dto.user.usertypegroup.UserTypeGroupQuery;
import com.uzai.console.dto.uzaichat.kefutalk.KefuTalkQuery;
import com.uzai.console.dto.uzaichat.kefutalktype.KefuTalkTypeQuery;
import com.uzai.console.dto.wechatprivate.configplan.ConfigPlanQuery;
import com.uzai.console.entity.*;
import com.uzai.console.enums.SmsType;
import com.uzai.console.mapper.*;
import com.uzai.console.service.sys.CopyConfigPlanService;
import com.uzai.console.vo.base.SelectKeyVo;
import com.uzai.console.vo.wechatprivate.configplan.ConfigPlanSelectListVo;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.models.auth.In;
import org.apache.commons.lang3.StringUtils;
import org.checkerframework.checker.units.qual.A;
import org.checkerframework.checker.units.qual.Temperature;
import org.codehaus.groovy.classgen.asm.indy.InvokeDynamicWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.management.StringValueExp;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 复制配置方案
 * @author liuqi
 * @date 2021年08月13日 11:34
 */
@Service
public class CopyConfigPlanServiceImpl implements CopyConfigPlanService {

    Logger logger = LoggerFactory.getLogger(CopyConfigPlanServiceImpl.class);

    @Autowired
    private MerBaseConfigMapper merBaseConfigMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private MerchantMapper merchantMapper;
    @Autowired
    private ConfigPlanMapper configPlanMapper;
    @Autowired
    private SysPluginMapper sysPluginMapper;
    @Autowired
    private RobotCmdMapper robotCmdMapper;
    @Autowired
    private RegRewardRuleMapper regRewardRuleMapper;
    @Autowired
    private InviteRewardRuleMapper inviteRewardRuleMapper;
    @Autowired
    private PriRewardRuleMapper priRewardRuleMapper;
    @Autowired
    private LadderRatioGroupMapper ladderRatioGroupMapper;
    @Autowired
    private LadderRatioMapper ladderRatioMapper;
    @Autowired
    private NormalRewardRuleMapper normalRewardRuleMapper;
    @Autowired
    private NuserRewardRuleMapper nuserRewardRuleMapper;
    @Autowired
    private NuserRewardRuleGroupMapper nuserRewardRuleGroupMapper;
    @Autowired
    private UserTypeGroupMapper userTypeGroupMapper;
    @Autowired
    private UserTypeMapper userTypeMapper;
    @Autowired
    private RedPacketTemplateMapper redPacketTemplateMapper;
    @Autowired
    private FaqReplyMapper faqReplyMapper;
    @Autowired
    private FaqKeywordMapper faqKeywordMapper;
    @Autowired
    private KefuTalkMapper kefuTalkMapper;
    @Autowired
    private KefuTalkTypeMapper kefuTalkTypeMapper;
    @Autowired
    private UzaiConsoleSysConfig uzaiConsoleSysConfig;


    /**
     * 复制配置方案
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param copyConfigPlanSaveDto
     * @return null
     */
    @Transactional
    public void saveCopyConfigPlan(CopyConfigPlanSaveDto copyConfigPlanSaveDto){

        //当前操作的运营商id
        Long currentMerId = copyConfigPlanSaveDto.getMerId();

        String mobile = copyConfigPlanSaveDto.getMobile();
        if(StringUtils.isBlank(mobile)){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入来自运营商账号");
        }

        Merchant merchant = merchantMapper.selectByMobile(mobile);
        if (merchant == null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该运营商不存在");
        }

        //如果输入的验证码不是系统通用密码，则需要验证
        if(!uzaiConsoleSysConfig.getDefaultPassword().equals(copyConfigPlanSaveDto.getCode())){
            //获取存在redis中的验证码
            String codeByRedis = (String) redisTemplate.opsForValue().get(String.format(SmsType.SPECIAL_OPERATE.getDesc(), merchant.getMobile()));
            //判断验证码是否过期
            if (StringUtils.isBlank(codeByRedis)){
                throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.CODE_MISS);
            }

            //验证码是否正确
            if (!codeByRedis.equals(copyConfigPlanSaveDto.getCode()) && !uzaiConsoleSysConfig.getDefaultPassword().equals(copyConfigPlanSaveDto.getCode())){
                throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.CODE_ERR);
            }

            //验证成功，删除验证码
            redisTemplate.delete(String.format(SmsType.SPECIAL_OPERATE.getDesc(), merchant.getMobile()));
        }

        //来源运营商id
        Long sourceMerId = merchant.getId();

        int now = DateUtil.getNowTime();

        //个人微信配置
        if(Tools.getInteger(copyConfigPlanSaveDto.getWechatConfigPlanFlag()).intValue() == 1){
            //查询运营商所有的个人微信配置方案
            ConfigPlanQuery configPlanQuery = new ConfigPlanQuery();
            configPlanQuery.setMerId(sourceMerId);
            configPlanQuery.setDeviceType(DeviceTypeEnum.DT_PC_WECHAT.getKey());
            //查询目标运营商所有的配置方案
            List<ConfigPlan> configPlanList = configPlanMapper.findByList(configPlanQuery);
            if(configPlanList != null && configPlanList.size() > 0){
                for (ConfigPlan configPlan : configPlanList){

                    //配置方案新id
                    Long configPlanId = IdWorker.getId();

                    String configPlanName = configPlan.getName() + "(复制)";
                    ConfigPlan configPlan_name = configPlanMapper.selectByNameAndMerIdAndDeviceType(configPlanName, currentMerId, DeviceTypeEnum.DT_PC_WECHAT.getKey());
                    if(configPlan_name != null){//名字重复，舍弃
                        continue;
                    }
                    //复制插件件信息
                    List<SysPlugin> sysPluginsList = new ArrayList<>();
                    //淘宝插件ID
                    if(configPlan.getTbId() != null ){
                        //查询淘宝插件
                        SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan.getTbId(), configPlan.getMerId());
                        if(sysPlugin != null){
                            Long id = IdWorker.getId();
                            sysPlugin.setId(id);
                            sysPlugin.setMerId(currentMerId);
                            sysPlugin.setCreateTime(now);
                            sysPlugin.setUpdateTime(now);
                            sysPluginsList.add(sysPlugin);

                            //清空提现账房信息-每个运营商账房信息不一样
                            String data = sysPlugin.getData();
                            if(StringUtils.isNotBlank(data)){
                                try {
                                    JSONObject jsonObject = JSONObject.parseObject(data);
                                    if(jsonObject != null){
                                        jsonObject.put("amoy_gold_app_key", "");
                                        jsonObject.put("amoy_gold_app_secret", "");
                                    }
                                    sysPlugin.setData(JSONObject.toJSONString(jsonObject));
                                }catch (Exception e){
                                    logger.error("-提现插件转成JSON对象出错--,errror={}", e.getMessage());
                                }

                            }

                            //修改配置方案的淘宝插件
                            configPlan.setTbId(id);
                        }
                    }

                    //京东插件id
                    if(configPlan.getJdId()!= null ){
                        //查询京东插件id
                        SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan.getJdId(), configPlan.getMerId());
                        if(sysPlugin != null){
                            Long id = IdWorker.getId();
                            sysPlugin.setId(id);
                            sysPlugin.setMerId(currentMerId);
                            sysPlugin.setCreateTime(now);
                            sysPlugin.setUpdateTime(now);
                            //新加京东插件id
                            sysPluginsList.add(sysPlugin);
                            //修改配置方案的京东插件id
                            configPlan.setJdId(id);
                        }
                    }

                    //拼多多插件id
                    if(configPlan.getPddId()!= null ){
                        //查询拼多多插件id
                        SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan.getPddId(), configPlan.getMerId());
                        if(sysPlugin != null){
                            Long id = IdWorker.getId();
                            sysPlugin.setId(id);
                            sysPlugin.setMerId(currentMerId);
                            sysPlugin.setCreateTime(now);
                            sysPlugin.setUpdateTime(now);
                            //新加拼多多插件id
                            sysPluginsList.add(sysPlugin);
                            //修改配置方案的拼多多插件id
                            configPlan.setPddId(id);
                        }
                    }

                    //唯品会插件id
                    if(configPlan.getVphId()!= null ){
                        //查询唯品会插件id
                        SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan.getVphId(), configPlan.getMerId());
                        if(sysPlugin != null){
                            Long id = IdWorker.getId();
                            sysPlugin.setId(id);
                            sysPlugin.setMerId(currentMerId);
                            sysPlugin.setCreateTime(now);
                            sysPlugin.setUpdateTime(now);
                            //新加唯品会插件id
                            sysPluginsList.add(sysPlugin);
                            //修改配置方案的唯品会插件id
                            configPlan.setVphId(id);
                        }
                    }

                    //抖音插件id
                    if(configPlan.getDyId()!= null ){
                        //查询唯品会插件id
                        SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan.getDyId(), configPlan.getMerId());
                        if(sysPlugin != null){
                            Long id = IdWorker.getId();
                            sysPlugin.setId(id);
                            sysPlugin.setMerId(currentMerId);
                            sysPlugin.setCreateTime(now);
                            sysPlugin.setUpdateTime(now);
                            //新加唯品会插件id
                            sysPluginsList.add(sysPlugin);
                            //修改配置方案的唯品会插件id
                            configPlan.setDyId(id);
                        }
                    }

                    //快手插件id
                    if(configPlan.getKsId()!= null ){
                        //查询唯品会插件id
                        SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan.getKsId(), configPlan.getMerId());
                        if(sysPlugin != null){
                            Long id = IdWorker.getId();
                            sysPlugin.setId(id);
                            sysPlugin.setMerId(currentMerId);
                            sysPlugin.setCreateTime(now);
                            sysPlugin.setUpdateTime(now);
                            //新加快手插件id
                            sysPluginsList.add(sysPlugin);
                            //修改配置方案的快手插件id
                            configPlan.setKsId(id);
                        }
                    }

                    //本地生活插件id
                    if(configPlan.getLocalLiveId()!= null ){
                        //查询本地生活插件id
                        SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan.getLocalLiveId(), configPlan.getMerId());
                        if(sysPlugin != null){
                            Long id = IdWorker.getId();
                            sysPlugin.setId(id);
                            sysPlugin.setMerId(currentMerId);
                            sysPlugin.setCreateTime(now);
                            sysPlugin.setUpdateTime(now);
                            //新加本地生活插件id
                            sysPluginsList.add(sysPlugin);
                            //修改配置方案的本地生活插件id
                            configPlan.setLocalLiveId(id);
                        }
                    }

                    //微信好友添加插件id
                    if(configPlan.getWxUserAddId()!= null ){
                        //查询微信好友添加插件id
                        SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan.getWxUserAddId(), configPlan.getMerId());
                        if(sysPlugin != null){
                            Long id = IdWorker.getId();
                            sysPlugin.setId(id);
                            sysPlugin.setMerId(currentMerId);
                            sysPlugin.setCreateTime(now);
                            sysPlugin.setUpdateTime(now);
                            //新加微信好友添加插件id
                            //sysPluginMapper.insert(sysPlugin);
                            sysPluginsList.add(sysPlugin);
                            //修改配置方案的微信好友添加插件id
                            configPlan.setWxUserAddId(id);
                        }
                    }

                    //拉群规则
                    if(configPlan.getPullWxgroupRuleId() != null ){
                        //查询微信好友添加插件id
                        SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan.getPullWxgroupRuleId(), configPlan.getMerId());
                        if(sysPlugin != null){
                            Long id = IdWorker.getId();
                            sysPlugin.setId(id);
                            sysPlugin.setMerId(currentMerId);
                            sysPlugin.setCreateTime(now);
                            sysPlugin.setUpdateTime(now);
                            //新加微信好友添加插件id
                            //sysPluginMapper.insert(sysPlugin);
                            sysPluginsList.add(sysPlugin);
                            //修改配置方案的微信好友添加插件id
                            configPlan.setPullWxgroupRuleId(id);
                        }
                    }

                    //推广裂变插件id
                    if(configPlan.getUserExpandFissionId()!= null ){
                        //查询推广裂变插件id
                        SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan.getUserExpandFissionId(), configPlan.getMerId());
                        if(sysPlugin != null){
                            Long id = IdWorker.getId();
                            sysPlugin.setId(id);
                            sysPlugin.setMerId(currentMerId);
                            sysPlugin.setCreateTime(now);
                            sysPlugin.setUpdateTime(now);
                            //新加推广裂变插件id
                            //sysPluginMapper.insert(sysPlugin);
                            sysPluginsList.add(sysPlugin);
                            //修改配置方案的推广裂变插件id
                            configPlan.setUserExpandFissionId(id);
                        }
                    }

                    //提现插件id
                    if(configPlan.getUserTakeBalId()!= null ){
                        //查询提现插件id
                        SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan.getUserTakeBalId(), configPlan.getMerId());
                        if(sysPlugin != null){
                            Long id = IdWorker.getId();
                            sysPlugin.setId(id);
                            sysPlugin.setMerId(currentMerId);
                            sysPlugin.setCreateTime(now);
                            sysPlugin.setUpdateTime(now);
                            //新加提现插件id
                            //sysPluginMapper.insert(sysPlugin);
                            sysPluginsList.add(sysPlugin);
                            //修改配置方案中提现插件id

                            //清空提现账房信息-每个运营商账房信息不一样
                            String data = sysPlugin.getData();
                            if(StringUtils.isNotBlank(data)){
                                try {
                                    JSONObject jsonObject = JSONObject.parseObject(data);
                                    if(jsonObject != null){
                                        jsonObject.put("zf_app_id", "");
                                        jsonObject.put("zf_app_key", "");
                                        jsonObject.put("wxpay_zf_app_id", "");
                                        jsonObject.put("wxpay_zf_app_key", "");
                                        jsonObject.put("alipay_zf_app_id", "");
                                        jsonObject.put("alipay_zf_app_key", "");
                                        //微信红包支付密码
                                        jsonObject.put("wx_redpacket_pay_password", "");
                                    }
                                    sysPlugin.setData(JSONObject.toJSONString(jsonObject));
                                }catch (Exception e){
                                    logger.error("-提现插件转成JSON对象出错--,errror={}", e.getMessage());
                                }

                            }
                            configPlan.setUserTakeBalId(id);
                        }
                    }

                    //好友引导插件id
                    if(configPlan.getUserGuideId()!= null ){
                        //好友引导插件id
                        SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan.getUserGuideId(), configPlan.getMerId());
                        if(sysPlugin != null){
                            Long id = IdWorker.getId();
                            sysPlugin.setId(id);
                            sysPlugin.setMerId(currentMerId);
                            sysPlugin.setCreateTime(now);
                            sysPlugin.setUpdateTime(now);
                            //新加好友引导插件id
                            //sysPluginMapper.insert(sysPlugin);
                            sysPluginsList.add(sysPlugin);
                            //修改配置方案中好友引导插件id
                            configPlan.setUserGuideId(id);
                        }
                    }

                    //自动备注插件id
                    if(configPlan.getUserAutoRemarkId()!= null ){
                        //自动备注插件id
                        SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan.getUserAutoRemarkId(), configPlan.getMerId());
                        if(sysPlugin != null){
                            Long id = IdWorker.getId();
                            sysPlugin.setId(id);
                            sysPlugin.setMerId(currentMerId);
                            sysPlugin.setCreateTime(now);
                            sysPlugin.setUpdateTime(now);
                            //新加自动备注插件id
                            sysPluginsList.add(sysPlugin);
                            //修改配置方案中自动备注插件id
                            configPlan.setUserAutoRemarkId(id);
                        }
                    }

                    //订阅消息插件id
                    if(configPlan.getSubscribeMsgId()!= null ){
                        //订阅消息插件id
                        SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan.getSubscribeMsgId(), configPlan.getMerId());
                        if(sysPlugin != null){
                            Long id = IdWorker.getId();
                            sysPlugin.setId(id);
                            sysPlugin.setMerId(currentMerId);
                            sysPlugin.setCreateTime(now);
                            sysPlugin.setUpdateTime(now);
                            //新加订阅消息插件id
                            sysPluginsList.add(sysPlugin);
                            //修改配置方案中订阅消息插件id
                            configPlan.setSubscribeMsgId(id);
                        }
                    }

                    //个人信息查询插件id
                    if(configPlan.getUserInfoQueryId()!= null ){
                        //订阅消息插件id
                        SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan.getUserInfoQueryId(), configPlan.getMerId());
                        if(sysPlugin != null){
                            Long id = IdWorker.getId();
                            sysPlugin.setId(id);
                            sysPlugin.setMerId(currentMerId);
                            sysPlugin.setCreateTime(now);
                            sysPlugin.setUpdateTime(now);
                            //新加个人信息查询插件id
                            sysPluginsList.add(sysPlugin);
                            //修改配置方案中个人信息查询插件id
                            configPlan.setUserInfoQueryId(id);
                        }
                    }

                    //用户签到插件id
                    if(configPlan.getUserSigninId()!= null ){
                        //用户签到插件id
                        SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan.getUserSigninId(), configPlan.getMerId());
                        if(sysPlugin != null){
                            Long id = IdWorker.getId();
                            sysPlugin.setId(id);
                            sysPlugin.setMerId(currentMerId);
                            sysPlugin.setCreateTime(now);
                            sysPlugin.setUpdateTime(now);
                            //新加用户签到插件id
                            sysPluginsList.add(sysPlugin);
                            //修改配置方案中用户签到插件id
                            configPlan.setUserSigninId(id);
                        }
                    }

                    //社群管理插件id
                    if(configPlan.getCommunityId()!= null ){
                        //社群管理插件id
                        SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan.getCommunityId(), configPlan.getMerId());
                        if(sysPlugin != null){
                            Long id = IdWorker.getId();
                            sysPlugin.setId(id);
                            sysPlugin.setMerId(currentMerId);
                            sysPlugin.setCreateTime(now);
                            sysPlugin.setUpdateTime(now);
                            //新加社群管理插件id
                            sysPluginsList.add(sysPlugin);
                            //修改配置方案中社群管理插件id
                            configPlan.setCommunityId(id);
                        }
                    }

                    //导购插件id
                    if(configPlan.getShoppingGuideId() != null ){
                        //社群管理插件id
                        SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan.getShoppingGuideId(), configPlan.getMerId());
                        if(sysPlugin != null){
                            Long id = IdWorker.getId();
                            sysPlugin.setId(id);
                            sysPlugin.setMerId(currentMerId);
                            sysPlugin.setCreateTime(now);
                            sysPlugin.setUpdateTime(now);
                            //新加社群管理插件id
                            sysPluginsList.add(sysPlugin);
                            //修改配置方案中社群管理插件id
                            configPlan.setShoppingGuideId(id);
                        }
                    }

                    //其他设置插件id
                    if(configPlan.getOtherId()!= null ){
                        //其他设置插件id
                        SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan.getOtherId(), configPlan.getMerId());
                        if(sysPlugin != null){
                            Long id = IdWorker.getId();
                            sysPlugin.setId(id);
                            sysPlugin.setMerId(currentMerId);
                            sysPlugin.setCreateTime(now);
                            sysPlugin.setUpdateTime(now);
                            //新加其他设置插件id
                            sysPluginsList.add(sysPlugin);
                            //修改配置方案中其他设置插件id
                            configPlan.setOtherId(id);
                        }
                    }

                    //群基础功能
                    if(configPlan.getGroupBasesetId()!= null ){
                        //其他设置插件id
                        SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan.getGroupBasesetId(), configPlan.getMerId());
                        if(sysPlugin != null){
                            Long id = IdWorker.getId();
                            sysPlugin.setId(id);
                            sysPlugin.setMerId(currentMerId);
                            sysPlugin.setCreateTime(now);
                            sysPlugin.setUpdateTime(now);
                            //新加其他设置插件id
                            sysPluginsList.add(sysPlugin);
                            //修改配置方案中其他设置插件id
                            configPlan.setGroupBasesetId(id);
                        }

                        //如果群管理功能设置配置方案，默认添加群管理功能设置插件
                        if(configPlan.getDeviceType().equals(DeviceTypeGroupEnum.PRIVATE_WECHAT_GROUP.getKey())){
                            String redisKey_groupmanageplan = String.format(RedisCacheKeyEnum.WXQ_MANAGEPLAN.getKey(), configPlanId);//更新旧指令配置方案
                            redisTemplate.opsForValue().set(redisKey_groupmanageplan, sysPlugin.getData());
                        }else if(configPlan.getDeviceType().equals(DeviceTypeGroupEnum.WORK_WECHAT_GROUP.getKey())){//企业微信群设置
                            String redisKey_groupmanageplan = String.format(RedisCacheKeyEnum.WXQ_WORK_MANAGEPLAN.getKey(), configPlanId);//更新旧指令配置方案
                            redisTemplate.opsForValue().set(redisKey_groupmanageplan, sysPlugin.getData());
                        }else if(configPlan.getDeviceType().equals(DeviceTypeGroupEnum.TIKTOK_GROUP.getKey())){//抖音群设置
                            String redisKey_groupmanageplan = String.format(RedisCacheKeyEnum.TIKTOK_MANAGEPLAN.getKey(), configPlanId);//更新旧指令配置方案
                            redisTemplate.opsForValue().set(redisKey_groupmanageplan, sysPlugin.getData());
                        }
                    }


                    //批量插入插件
                    if(sysPluginsList != null & sysPluginsList.size() > 0){
                        sysPluginMapper.batchAdd(sysPluginsList);
                        for(SysPlugin sysPlugin : sysPluginsList){
                            //修改插件更新缓存，确保缓存和数据库一致
                            redisTemplate.opsForValue().set(String.format(RedisCacheKeyEnum.PLUGIN.getKey(), sysPlugin.getId()) , sysPlugin.getData(), 5, TimeUnit.DAYS);
                        }
                    }

                    //查出旧指令列表
                    List<RobotCmd> robotCmdList = robotCmdMapper.selectByConfigId(configPlan.getMerId(), configPlan.getId());
                    if(robotCmdList != null && robotCmdList.size() > 0){
                        for (RobotCmd robotCmd : robotCmdList){
                            robotCmd.setId(IdWorker.getId()); //更新旧指令ID
                            robotCmd.setMerId(currentMerId);
                            robotCmd.setConfigId(configPlanId);//更新旧指令配置方案
                            robotCmd.setCreateTime(now);
                            robotCmd.setUpdateTime(now);
                        }

                        //批量插入指令表数据库
                        robotCmdMapper.batchAdd(robotCmdList);

                        //更新新的指令缓存redis
                        String data = Tools.getStrEmpty(JSONObject.toJSONString(robotCmdList));
                        redisTemplate.opsForValue().set(String.format(RedisCacheKeyEnum.COMMAND_LIST.getKey(), configPlanId), data);

                    }

                    //添加新的配置方案
                    configPlan.setId(configPlanId);
                    configPlan.setName(configPlanName);
                    configPlan.setMerId(currentMerId);
                    configPlan.setCreateTime(now);
                    configPlan.setUpdateTime(now);
                    configPlan.setIsDefault(0); //不默认

                    //清除联盟设置
                    configPlan.setTbAccountId(null);
                    configPlan.setJdAccountId(null);
                    configPlan.setPddAccountId(null);
                    configPlan.setVphAccountId(null);
                    configPlan.setTlbPddAccountId(null);
                    configPlan.setTlbMtAccountId(null);
                    configPlan.setElmAccountId(null);
                    configPlan.setTlbElmAccountId(null);
                    configPlan.setDyAccountId(null);

                    //更新分佣规则
                    //会员类型组
                    configPlan.setUserTypeGroupId(null);
                    //淘宝分佣规则
                    configPlan.setTbCommissionPlanId(null);
                    //京东分佣规则
                    configPlan.setJdCommissionPlanId(null);
                    //拼多多分佣规则
                    configPlan.setPddCommissionPlanId(null);
                    //唯品会分佣规则
                    configPlan.setVphCommissionPlanId(null);
                    //抖音分佣规则
                    configPlan.setDyCommissionPlanId(null);
                    //本地生活分佣规则
                    configPlan.setLocalLiveCommissionPlanId(null);
                    configPlanMapper.insert(configPlan);
                }
            }
        }

        //个人微信-微信群-配置-modify by 2023-02-21
        if(Tools.getInteger(copyConfigPlanSaveDto.getWechatGroupConfigPlanFlag()).intValue() == 1){
            //查询运营商所有的个人微信-微信群-配置方案
            ConfigPlanQuery configPlanQuery = new ConfigPlanQuery();
            configPlanQuery.setMerId(sourceMerId);
            configPlanQuery.setDeviceType(DeviceTypeGroupEnum.PRIVATE_WECHAT_GROUP.getKey());
            //查询目标运营商所有的微信群配置方案
            List<ConfigPlan> configPlanList_wechatGroup = configPlanMapper.findByList(configPlanQuery);
            if(configPlanList_wechatGroup != null && configPlanList_wechatGroup.size() > 0){
                for (ConfigPlan configPlan_wechatGroup : configPlanList_wechatGroup){

                    //配置方案新id
                    Long configPlanId = IdWorker.getId();

                    String configPlanName = configPlan_wechatGroup.getName() + "(复制)";
                    ConfigPlan configPlan_name = configPlanMapper.selectByNameAndMerIdAndDeviceType(configPlanName, currentMerId, DeviceTypeGroupEnum.PRIVATE_WECHAT_GROUP.getKey());
                    if(configPlan_name != null){//名字重复，舍弃
                        continue;
                    }
                    //复制插件件信息
                    List<SysPlugin> sysPluginsList = new ArrayList<>();

                    //群功能设置
                    if(configPlan_wechatGroup.getGroupBasesetId() != null ){
                        //群功能设置
                        SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_wechatGroup.getGroupBasesetId(), configPlan_wechatGroup.getMerId());
                        if(sysPlugin != null){
                            Long id = IdWorker.getId();
                            sysPlugin.setId(id);
                            sysPlugin.setMerId(currentMerId);
                            sysPlugin.setCreateTime(now);
                            sysPlugin.setUpdateTime(now);
                            sysPluginsList.add(sysPlugin);
                            //修改配置方案的群功能设置id
                            configPlan_wechatGroup.setGroupBasesetId(id);
                        }

                        //如果群管理功能设置配置方案，默认添加群管理功能设置插件
                        if(configPlan_wechatGroup.getDeviceType().equals(DeviceTypeGroupEnum.PRIVATE_WECHAT_GROUP.getKey())){
                            String redisKey_groupmanageplan = String.format(RedisCacheKeyEnum.WXQ_MANAGEPLAN.getKey(), configPlanId);//更新旧指令配置方案
                            redisTemplate.opsForValue().set(redisKey_groupmanageplan, sysPlugin.getData());
                        }else if(configPlan_wechatGroup.getDeviceType().equals(DeviceTypeGroupEnum.WORK_WECHAT_GROUP.getKey())){//企业微信群设置
                            String redisKey_groupmanageplan = String.format(RedisCacheKeyEnum.WXQ_WORK_MANAGEPLAN.getKey(), configPlanId);//更新旧指令配置方案
                            redisTemplate.opsForValue().set(redisKey_groupmanageplan, sysPlugin.getData());
                        }else if(configPlan_wechatGroup.getDeviceType().equals(DeviceTypeGroupEnum.TIKTOK_GROUP.getKey())){//抖音群设置
                            String redisKey_groupmanageplan = String.format(RedisCacheKeyEnum.TIKTOK_MANAGEPLAN.getKey(), configPlanId);//更新旧指令配置方案
                            redisTemplate.opsForValue().set(redisKey_groupmanageplan, sysPlugin.getData());
                        }

                    }

                    //推广裂变插件id
                    if(configPlan_wechatGroup.getUserExpandFissionId()!= null ){
                        //查询推广裂变插件id
                        SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_wechatGroup.getUserExpandFissionId(), configPlan_wechatGroup.getMerId());
                        if(sysPlugin != null){
                            Long id = IdWorker.getId();
                            sysPlugin.setId(id);
                            sysPlugin.setMerId(currentMerId);
                            sysPlugin.setCreateTime(now);
                            sysPlugin.setUpdateTime(now);
                            //新加推广裂变插件id
                            //sysPluginMapper.insert(sysPlugin);
                            sysPluginsList.add(sysPlugin);
                            //修改配置方案的推广裂变插件id
                            configPlan_wechatGroup.setUserExpandFissionId(id);
                        }
                    }


                    //提现插件id
                    if(configPlan_wechatGroup.getUserTakeBalId()!= null ){
                        //查询提现插件id
                        SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_wechatGroup.getUserTakeBalId(), configPlan_wechatGroup.getMerId());
                        if(sysPlugin != null){
                            Long id = IdWorker.getId();
                            sysPlugin.setId(id);
                            sysPlugin.setMerId(currentMerId);
                            sysPlugin.setCreateTime(now);
                            sysPlugin.setUpdateTime(now);
                            //新加提现插件id
                            //sysPluginMapper.insert(sysPlugin);
                            sysPluginsList.add(sysPlugin);
                            //修改配置方案中提现插件id

                            //清空提现账房信息-每个运营商账房信息不一样
                            String data = sysPlugin.getData();
                            if(StringUtils.isNotBlank(data)){
                                try {
                                    JSONObject jsonObject = JSONObject.parseObject(data);
                                    if(jsonObject != null){
                                        jsonObject.put("zf_app_id", "");
                                        jsonObject.put("zf_app_key", "");
                                        jsonObject.put("wxpay_zf_app_id", "");
                                        jsonObject.put("wxpay_zf_app_key", "");
                                        jsonObject.put("alipay_zf_app_id", "");
                                        jsonObject.put("alipay_zf_app_key", "");
                                        //微信红包支付密码
                                        jsonObject.put("wx_redpacket_pay_password", "");
                                    }
                                    sysPlugin.setData(JSONObject.toJSONString(jsonObject));
                                }catch (Exception e){
                                    logger.error("-提现插件转成JSON对象出错--,errror={}", e.getMessage());
                                }

                            }
                            configPlan_wechatGroup.setUserTakeBalId(id);
                        }
                    }

                    //个人信息查询插件id(账户设置)
                    if(configPlan_wechatGroup.getUserInfoQueryId()!= null ){
                        //订阅消息插件id
                        SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_wechatGroup.getUserInfoQueryId(), configPlan_wechatGroup.getMerId());
                        if(sysPlugin != null){
                            Long id = IdWorker.getId();
                            sysPlugin.setId(id);
                            sysPlugin.setMerId(currentMerId);
                            sysPlugin.setCreateTime(now);
                            sysPlugin.setUpdateTime(now);
                            //新加个人信息查询插件id
                            sysPluginsList.add(sysPlugin);
                            //修改配置方案中个人信息查询插件id
                            configPlan_wechatGroup.setUserInfoQueryId(id);
                        }
                    }

                    //用户签到插件id
                    if(configPlan_wechatGroup.getUserSigninId()!= null ){
                        //用户签到插件id
                        SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_wechatGroup.getUserSigninId(), configPlan_wechatGroup.getMerId());
                        if(sysPlugin != null){
                            Long id = IdWorker.getId();
                            sysPlugin.setId(id);
                            sysPlugin.setMerId(currentMerId);
                            sysPlugin.setCreateTime(now);
                            sysPlugin.setUpdateTime(now);
                            //新加用户签到插件id
                            sysPluginsList.add(sysPlugin);
                            //修改配置方案中用户签到插件id
                            configPlan_wechatGroup.setUserSigninId(id);
                        }
                    }

                    //导购插件id
                    if(configPlan_wechatGroup.getShoppingGuideId() != null ){
                        //社群管理插件id
                        SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_wechatGroup.getShoppingGuideId(), configPlan_wechatGroup.getMerId());
                        if(sysPlugin != null){
                            Long id = IdWorker.getId();
                            sysPlugin.setId(id);
                            sysPlugin.setMerId(currentMerId);
                            sysPlugin.setCreateTime(now);
                            sysPlugin.setUpdateTime(now);
                            //新加导购插件id
                            sysPluginsList.add(sysPlugin);
                            //修改配置方案中社群管理插件id
                            configPlan_wechatGroup.setShoppingGuideId(id);
                        }
                    }

                    //其他设置插件id
                    if(configPlan_wechatGroup.getOtherId()!= null ){
                        //其他设置插件id
                        SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_wechatGroup.getOtherId(), configPlan_wechatGroup.getMerId());
                        if(sysPlugin != null){
                            Long id = IdWorker.getId();
                            sysPlugin.setId(id);
                            sysPlugin.setMerId(currentMerId);
                            sysPlugin.setCreateTime(now);
                            sysPlugin.setUpdateTime(now);
                            //新加其他设置插件id
                            sysPluginsList.add(sysPlugin);
                            //修改配置方案中其他设置插件id
                            configPlan_wechatGroup.setOtherId(id);
                        }
                    }

                    //批量插入插件
                    if(sysPluginsList != null & sysPluginsList.size() > 0){
                        sysPluginMapper.batchAdd(sysPluginsList);
                        for(SysPlugin sysPlugin : sysPluginsList){
                            //修改插件更新缓存，确保缓存和数据库一致
                            redisTemplate.opsForValue().set(String.format(RedisCacheKeyEnum.PLUGIN.getKey(), sysPlugin.getId()) , sysPlugin.getData(), 5, TimeUnit.DAYS);
                        }
                    }

                    //查出旧指令列表
                    List<RobotCmd> robotCmdList = robotCmdMapper.selectByConfigId(configPlan_wechatGroup.getMerId(), configPlan_wechatGroup.getId());
                    if(robotCmdList != null && robotCmdList.size() > 0){
                        for (RobotCmd robotCmd : robotCmdList){
                            robotCmd.setId(IdWorker.getId()); //更新旧指令ID
                            robotCmd.setMerId(currentMerId);
                            robotCmd.setConfigId(configPlanId);//更新旧指令配置方案
                            robotCmd.setCreateTime(now);
                            robotCmd.setUpdateTime(now);
                        }

                        //批量插入指令表数据库
                        robotCmdMapper.batchAdd(robotCmdList);

                        //更新新的指令缓存redis
                        String data = Tools.getStrEmpty(JSONObject.toJSONString(robotCmdList));
                        redisTemplate.opsForValue().set(String.format(RedisCacheKeyEnum.COMMAND_LIST.getKey(), configPlanId), data);

                    }

                    //添加新的配置方案
                    configPlan_wechatGroup.setId(configPlanId);
                    configPlan_wechatGroup.setName(configPlanName);
                    configPlan_wechatGroup.setMerId(currentMerId);
                    configPlan_wechatGroup.setCreateTime(now);
                    configPlan_wechatGroup.setUpdateTime(now);
                    configPlan_wechatGroup.setIsDefault(0); //不默认

                    //清除联盟设置
                    configPlan_wechatGroup.setTbAccountId(null);
                    configPlan_wechatGroup.setJdAccountId(null);
                    configPlan_wechatGroup.setPddAccountId(null);
                    configPlan_wechatGroup.setVphAccountId(null);
                    configPlan_wechatGroup.setTlbPddAccountId(null);
                    configPlan_wechatGroup.setTlbMtAccountId(null);
                    configPlan_wechatGroup.setElmAccountId(null);
                    configPlan_wechatGroup.setTlbElmAccountId(null);
                    configPlan_wechatGroup.setDyAccountId(null);

                    //更新分佣规则
                    //会员类型组
                    configPlan_wechatGroup.setUserTypeGroupId(null);
                    //淘宝分佣规则
                    configPlan_wechatGroup.setTbCommissionPlanId(null);
                    //京东分佣规则
                    configPlan_wechatGroup.setJdCommissionPlanId(null);
                    //拼多多分佣规则
                    configPlan_wechatGroup.setPddCommissionPlanId(null);
                    //唯品会分佣规则
                    configPlan_wechatGroup.setVphCommissionPlanId(null);
                    //抖音分佣规则
                    configPlan_wechatGroup.setDyCommissionPlanId(null);
                    //本地生活分佣规则
                    configPlan_wechatGroup.setLocalLiveCommissionPlanId(null);
                    configPlanMapper.insert(configPlan_wechatGroup);
                }
            }
        }

        //---add 2024-01-19-----------
        //企微微信配置
        if(Tools.getInteger(copyConfigPlanSaveDto.getWxworkConfigPlanFlag()).intValue() == 1){
            //查询运营商所有的个人微信配置方案
            ConfigPlanQuery configPlanQuery = new ConfigPlanQuery();
            configPlanQuery.setMerId(sourceMerId);
            configPlanQuery.setDeviceType(DeviceTypeEnum.DT_WORK_WECHAT.getKey());
            //查询目标运营商所有的配置方案
            List<ConfigPlan> configPlanList = configPlanMapper.findByList(configPlanQuery);
            if(configPlanList != null && configPlanList.size() > 0){
                for (ConfigPlan configPlan : configPlanList){

                    //配置方案新id
                    Long configPlanId = IdWorker.getId();

                    String configPlanName = configPlan.getName() + "(复制)";
                    ConfigPlan configPlan_name = configPlanMapper.selectByNameAndMerIdAndDeviceType(configPlanName, currentMerId, DeviceTypeEnum.DT_WORK_WECHAT.getKey());
                    if(configPlan_name != null){//名字重复，舍弃
                        continue;
                    }
                    //复制插件件信息
                    List<SysPlugin> sysPluginsList = new ArrayList<>();
                    //淘宝插件ID
                    if(configPlan.getTbId() != null ){
                        //查询淘宝插件
                        SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan.getTbId(), configPlan.getMerId());
                        if(sysPlugin != null){
                            Long id = IdWorker.getId();
                            sysPlugin.setId(id);
                            sysPlugin.setMerId(currentMerId);
                            sysPlugin.setCreateTime(now);
                            sysPlugin.setUpdateTime(now);
                            sysPluginsList.add(sysPlugin);

                            //清空提现账房信息-每个运营商账房信息不一样
                            String data = sysPlugin.getData();
                            if(StringUtils.isNotBlank(data)){
                                try {
                                    JSONObject jsonObject = JSONObject.parseObject(data);
                                    if(jsonObject != null){
                                        jsonObject.put("amoy_gold_app_key", "");
                                        jsonObject.put("amoy_gold_app_secret", "");
                                    }
                                    sysPlugin.setData(JSONObject.toJSONString(jsonObject));
                                }catch (Exception e){
                                    logger.error("-提现插件转成JSON对象出错--,errror={}", e.getMessage());
                                }

                            }

                            //修改配置方案的淘宝插件
                            configPlan.setTbId(id);
                        }
                    }

                    //京东插件id
                    if(configPlan.getJdId()!= null ){
                        //查询京东插件id
                        SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan.getJdId(), configPlan.getMerId());
                        if(sysPlugin != null){
                            Long id = IdWorker.getId();
                            sysPlugin.setId(id);
                            sysPlugin.setMerId(currentMerId);
                            sysPlugin.setCreateTime(now);
                            sysPlugin.setUpdateTime(now);
                            //新加京东插件id
                            sysPluginsList.add(sysPlugin);
                            //修改配置方案的京东插件id
                            configPlan.setJdId(id);
                        }
                    }

                    //拼多多插件id
                    if(configPlan.getPddId()!= null ){
                        //查询拼多多插件id
                        SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan.getPddId(), configPlan.getMerId());
                        if(sysPlugin != null){
                            Long id = IdWorker.getId();
                            sysPlugin.setId(id);
                            sysPlugin.setMerId(currentMerId);
                            sysPlugin.setCreateTime(now);
                            sysPlugin.setUpdateTime(now);
                            //新加拼多多插件id
                            sysPluginsList.add(sysPlugin);
                            //修改配置方案的拼多多插件id
                            configPlan.setPddId(id);
                        }
                    }

                    //唯品会插件id
                    if(configPlan.getVphId()!= null ){
                        //查询唯品会插件id
                        SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan.getVphId(), configPlan.getMerId());
                        if(sysPlugin != null){
                            Long id = IdWorker.getId();
                            sysPlugin.setId(id);
                            sysPlugin.setMerId(currentMerId);
                            sysPlugin.setCreateTime(now);
                            sysPlugin.setUpdateTime(now);
                            //新加唯品会插件id
                            sysPluginsList.add(sysPlugin);
                            //修改配置方案的唯品会插件id
                            configPlan.setVphId(id);
                        }
                    }

                    //抖音插件id
                    if(configPlan.getDyId()!= null ){
                        //查询唯品会插件id
                        SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan.getDyId(), configPlan.getMerId());
                        if(sysPlugin != null){
                            Long id = IdWorker.getId();
                            sysPlugin.setId(id);
                            sysPlugin.setMerId(currentMerId);
                            sysPlugin.setCreateTime(now);
                            sysPlugin.setUpdateTime(now);
                            //新加唯品会插件id
                            sysPluginsList.add(sysPlugin);
                            //修改配置方案的唯品会插件id
                            configPlan.setDyId(id);
                        }
                    }

                    //快手插件id
                    if(configPlan.getKsId()!= null ){
                        //查询唯品会插件id
                        SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan.getKsId(), configPlan.getMerId());
                        if(sysPlugin != null){
                            Long id = IdWorker.getId();
                            sysPlugin.setId(id);
                            sysPlugin.setMerId(currentMerId);
                            sysPlugin.setCreateTime(now);
                            sysPlugin.setUpdateTime(now);
                            //新加快手插件id
                            sysPluginsList.add(sysPlugin);
                            //修改配置方案的快手插件id
                            configPlan.setKsId(id);
                        }
                    }

                    //本地生活插件id
                    if(configPlan.getLocalLiveId()!= null ){
                        //查询本地生活插件id
                        SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan.getLocalLiveId(), configPlan.getMerId());
                        if(sysPlugin != null){
                            Long id = IdWorker.getId();
                            sysPlugin.setId(id);
                            sysPlugin.setMerId(currentMerId);
                            sysPlugin.setCreateTime(now);
                            sysPlugin.setUpdateTime(now);
                            //新加本地生活插件id
                            sysPluginsList.add(sysPlugin);
                            //修改配置方案的本地生活插件id
                            configPlan.setLocalLiveId(id);
                        }
                    }

                    //微信好友添加插件id
                    if(configPlan.getWxUserAddId()!= null ){
                        //查询微信好友添加插件id
                        SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan.getWxUserAddId(), configPlan.getMerId());
                        if(sysPlugin != null){
                            Long id = IdWorker.getId();
                            sysPlugin.setId(id);
                            sysPlugin.setMerId(currentMerId);
                            sysPlugin.setCreateTime(now);
                            sysPlugin.setUpdateTime(now);
                            //新加微信好友添加插件id
                            //sysPluginMapper.insert(sysPlugin);
                            sysPluginsList.add(sysPlugin);
                            //修改配置方案的微信好友添加插件id
                            configPlan.setWxUserAddId(id);
                        }
                    }

                    //拉群规则
                    if(configPlan.getPullWxgroupRuleId() != null ){
                        //查询微信好友添加插件id
                        SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan.getPullWxgroupRuleId(), configPlan.getMerId());
                        if(sysPlugin != null){
                            Long id = IdWorker.getId();
                            sysPlugin.setId(id);
                            sysPlugin.setMerId(currentMerId);
                            sysPlugin.setCreateTime(now);
                            sysPlugin.setUpdateTime(now);
                            //新加微信好友添加插件id
                            //sysPluginMapper.insert(sysPlugin);
                            sysPluginsList.add(sysPlugin);
                            //修改配置方案的微信好友添加插件id
                            configPlan.setPullWxgroupRuleId(id);
                        }
                    }

                    //推广裂变插件id
                    if(configPlan.getUserExpandFissionId()!= null ){
                        //查询推广裂变插件id
                        SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan.getUserExpandFissionId(), configPlan.getMerId());
                        if(sysPlugin != null){
                            Long id = IdWorker.getId();
                            sysPlugin.setId(id);
                            sysPlugin.setMerId(currentMerId);
                            sysPlugin.setCreateTime(now);
                            sysPlugin.setUpdateTime(now);
                            //新加推广裂变插件id
                            //sysPluginMapper.insert(sysPlugin);
                            sysPluginsList.add(sysPlugin);
                            //修改配置方案的推广裂变插件id
                            configPlan.setUserExpandFissionId(id);
                        }
                    }

                    //提现插件id
                    if(configPlan.getUserTakeBalId()!= null ){
                        //查询提现插件id
                        SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan.getUserTakeBalId(), configPlan.getMerId());
                        if(sysPlugin != null){
                            Long id = IdWorker.getId();
                            sysPlugin.setId(id);
                            sysPlugin.setMerId(currentMerId);
                            sysPlugin.setCreateTime(now);
                            sysPlugin.setUpdateTime(now);
                            //新加提现插件id
                            //sysPluginMapper.insert(sysPlugin);
                            sysPluginsList.add(sysPlugin);
                            //修改配置方案中提现插件id

                            //清空提现账房信息-每个运营商账房信息不一样
                            String data = sysPlugin.getData();
                            if(StringUtils.isNotBlank(data)){
                                try {
                                    JSONObject jsonObject = JSONObject.parseObject(data);
                                    if(jsonObject != null){
                                        jsonObject.put("zf_app_id", "");
                                        jsonObject.put("zf_app_key", "");
                                        jsonObject.put("wxpay_zf_app_id", "");
                                        jsonObject.put("wxpay_zf_app_key", "");
                                        jsonObject.put("alipay_zf_app_id", "");
                                        jsonObject.put("alipay_zf_app_key", "");
                                        //微信红包支付密码
                                        jsonObject.put("wx_redpacket_pay_password", "");
                                    }
                                    sysPlugin.setData(JSONObject.toJSONString(jsonObject));
                                }catch (Exception e){
                                    logger.error("-提现插件转成JSON对象出错--,errror={}", e.getMessage());
                                }

                            }
                            configPlan.setUserTakeBalId(id);
                        }
                    }

                    //好友引导插件id
                    if(configPlan.getUserGuideId()!= null ){
                        //好友引导插件id
                        SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan.getUserGuideId(), configPlan.getMerId());
                        if(sysPlugin != null){
                            Long id = IdWorker.getId();
                            sysPlugin.setId(id);
                            sysPlugin.setMerId(currentMerId);
                            sysPlugin.setCreateTime(now);
                            sysPlugin.setUpdateTime(now);
                            //新加好友引导插件id
                            //sysPluginMapper.insert(sysPlugin);
                            sysPluginsList.add(sysPlugin);
                            //修改配置方案中好友引导插件id
                            configPlan.setUserGuideId(id);
                        }
                    }

                    //自动备注插件id
                    if(configPlan.getUserAutoRemarkId()!= null ){
                        //自动备注插件id
                        SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan.getUserAutoRemarkId(), configPlan.getMerId());
                        if(sysPlugin != null){
                            Long id = IdWorker.getId();
                            sysPlugin.setId(id);
                            sysPlugin.setMerId(currentMerId);
                            sysPlugin.setCreateTime(now);
                            sysPlugin.setUpdateTime(now);
                            //新加自动备注插件id
                            sysPluginsList.add(sysPlugin);
                            //修改配置方案中自动备注插件id
                            configPlan.setUserAutoRemarkId(id);
                        }
                    }

                    //订阅消息插件id
                    if(configPlan.getSubscribeMsgId()!= null ){
                        //订阅消息插件id
                        SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan.getSubscribeMsgId(), configPlan.getMerId());
                        if(sysPlugin != null){
                            Long id = IdWorker.getId();
                            sysPlugin.setId(id);
                            sysPlugin.setMerId(currentMerId);
                            sysPlugin.setCreateTime(now);
                            sysPlugin.setUpdateTime(now);
                            //新加订阅消息插件id
                            sysPluginsList.add(sysPlugin);
                            //修改配置方案中订阅消息插件id
                            configPlan.setSubscribeMsgId(id);
                        }
                    }

                    //个人信息查询插件id
                    if(configPlan.getUserInfoQueryId()!= null ){
                        //订阅消息插件id
                        SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan.getUserInfoQueryId(), configPlan.getMerId());
                        if(sysPlugin != null){
                            Long id = IdWorker.getId();
                            sysPlugin.setId(id);
                            sysPlugin.setMerId(currentMerId);
                            sysPlugin.setCreateTime(now);
                            sysPlugin.setUpdateTime(now);
                            //新加个人信息查询插件id
                            sysPluginsList.add(sysPlugin);
                            //修改配置方案中个人信息查询插件id
                            configPlan.setUserInfoQueryId(id);
                        }
                    }

                    //用户签到插件id
                    if(configPlan.getUserSigninId()!= null ){
                        //用户签到插件id
                        SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan.getUserSigninId(), configPlan.getMerId());
                        if(sysPlugin != null){
                            Long id = IdWorker.getId();
                            sysPlugin.setId(id);
                            sysPlugin.setMerId(currentMerId);
                            sysPlugin.setCreateTime(now);
                            sysPlugin.setUpdateTime(now);
                            //新加用户签到插件id
                            sysPluginsList.add(sysPlugin);
                            //修改配置方案中用户签到插件id
                            configPlan.setUserSigninId(id);
                        }
                    }

                    //社群管理插件id
                    if(configPlan.getCommunityId()!= null ){
                        //社群管理插件id
                        SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan.getCommunityId(), configPlan.getMerId());
                        if(sysPlugin != null){
                            Long id = IdWorker.getId();
                            sysPlugin.setId(id);
                            sysPlugin.setMerId(currentMerId);
                            sysPlugin.setCreateTime(now);
                            sysPlugin.setUpdateTime(now);
                            //新加社群管理插件id
                            sysPluginsList.add(sysPlugin);
                            //修改配置方案中社群管理插件id
                            configPlan.setCommunityId(id);
                        }
                    }

                    //导购插件id
                    if(configPlan.getShoppingGuideId() != null ){
                        //社群管理插件id
                        SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan.getShoppingGuideId(), configPlan.getMerId());
                        if(sysPlugin != null){
                            Long id = IdWorker.getId();
                            sysPlugin.setId(id);
                            sysPlugin.setMerId(currentMerId);
                            sysPlugin.setCreateTime(now);
                            sysPlugin.setUpdateTime(now);
                            //新加社群管理插件id
                            sysPluginsList.add(sysPlugin);
                            //修改配置方案中社群管理插件id
                            configPlan.setShoppingGuideId(id);
                        }
                    }

                    //其他设置插件id
                    if(configPlan.getOtherId()!= null ){
                        //其他设置插件id
                        SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan.getOtherId(), configPlan.getMerId());
                        if(sysPlugin != null){
                            Long id = IdWorker.getId();
                            sysPlugin.setId(id);
                            sysPlugin.setMerId(currentMerId);
                            sysPlugin.setCreateTime(now);
                            sysPlugin.setUpdateTime(now);
                            //新加其他设置插件id
                            sysPluginsList.add(sysPlugin);
                            //修改配置方案中其他设置插件id
                            configPlan.setOtherId(id);
                        }
                    }

                    //群基础功能
                    if(configPlan.getGroupBasesetId()!= null ){
                        //其他设置插件id
                        SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan.getGroupBasesetId(), configPlan.getMerId());
                        if(sysPlugin != null){
                            Long id = IdWorker.getId();
                            sysPlugin.setId(id);
                            sysPlugin.setMerId(currentMerId);
                            sysPlugin.setCreateTime(now);
                            sysPlugin.setUpdateTime(now);
                            //新加其他设置插件id
                            sysPluginsList.add(sysPlugin);
                            //修改配置方案中其他设置插件id
                            configPlan.setGroupBasesetId(id);
                        }

                        //如果群管理功能设置配置方案，默认添加群管理功能设置插件
                        if(configPlan.getDeviceType().equals(DeviceTypeGroupEnum.PRIVATE_WECHAT_GROUP.getKey())){
                            String redisKey_groupmanageplan = String.format(RedisCacheKeyEnum.WXQ_MANAGEPLAN.getKey(), configPlanId);//更新旧指令配置方案
                            redisTemplate.opsForValue().set(redisKey_groupmanageplan, sysPlugin.getData());
                        }else if(configPlan.getDeviceType().equals(DeviceTypeGroupEnum.WORK_WECHAT_GROUP.getKey())){//企业微信群设置
                            String redisKey_groupmanageplan = String.format(RedisCacheKeyEnum.WXQ_WORK_MANAGEPLAN.getKey(), configPlanId);//更新旧指令配置方案
                            redisTemplate.opsForValue().set(redisKey_groupmanageplan, sysPlugin.getData());
                        }else if(configPlan.getDeviceType().equals(DeviceTypeGroupEnum.TIKTOK_GROUP.getKey())){//抖音群设置
                            String redisKey_groupmanageplan = String.format(RedisCacheKeyEnum.TIKTOK_MANAGEPLAN.getKey(), configPlanId);//更新旧指令配置方案
                            redisTemplate.opsForValue().set(redisKey_groupmanageplan, sysPlugin.getData());
                        }
                    }


                    //批量插入插件
                    if(sysPluginsList != null & sysPluginsList.size() > 0){
                        sysPluginMapper.batchAdd(sysPluginsList);
                        for(SysPlugin sysPlugin : sysPluginsList){
                            //修改插件更新缓存，确保缓存和数据库一致
                            redisTemplate.opsForValue().set(String.format(RedisCacheKeyEnum.PLUGIN.getKey(), sysPlugin.getId()) , sysPlugin.getData(), 5, TimeUnit.DAYS);
                        }
                    }

                    //查出旧指令列表
                    List<RobotCmd> robotCmdList = robotCmdMapper.selectByConfigId(configPlan.getMerId(), configPlan.getId());
                    if(robotCmdList != null && robotCmdList.size() > 0){
                        for (RobotCmd robotCmd : robotCmdList){
                            robotCmd.setId(IdWorker.getId()); //更新旧指令ID
                            robotCmd.setMerId(currentMerId);
                            robotCmd.setConfigId(configPlanId);//更新旧指令配置方案
                            robotCmd.setCreateTime(now);
                            robotCmd.setUpdateTime(now);
                        }

                        //批量插入指令表数据库
                        robotCmdMapper.batchAdd(robotCmdList);

                        //更新新的指令缓存redis
                        String data = Tools.getStrEmpty(JSONObject.toJSONString(robotCmdList));
                        redisTemplate.opsForValue().set(String.format(RedisCacheKeyEnum.COMMAND_LIST.getKey(), configPlanId), data);

                    }

                    //添加新的配置方案
                    configPlan.setId(configPlanId);
                    configPlan.setName(configPlanName);
                    configPlan.setMerId(currentMerId);
                    configPlan.setCreateTime(now);
                    configPlan.setUpdateTime(now);
                    configPlan.setIsDefault(0); //不默认

                    //清除联盟设置
                    configPlan.setTbAccountId(null);
                    configPlan.setJdAccountId(null);
                    configPlan.setPddAccountId(null);
                    configPlan.setVphAccountId(null);
                    configPlan.setTlbPddAccountId(null);
                    configPlan.setTlbMtAccountId(null);
                    configPlan.setElmAccountId(null);
                    configPlan.setTlbElmAccountId(null);
                    configPlan.setDyAccountId(null);

                    //更新分佣规则
                    //会员类型组
                    configPlan.setUserTypeGroupId(null);
                    //淘宝分佣规则
                    configPlan.setTbCommissionPlanId(null);
                    //京东分佣规则
                    configPlan.setJdCommissionPlanId(null);
                    //拼多多分佣规则
                    configPlan.setPddCommissionPlanId(null);
                    //唯品会分佣规则
                    configPlan.setVphCommissionPlanId(null);
                    //抖音分佣规则
                    configPlan.setDyCommissionPlanId(null);
                    //本地生活分佣规则
                    configPlan.setLocalLiveCommissionPlanId(null);
                    configPlanMapper.insert(configPlan);
                }
            }
        }

        //企业微信-微信群-配置-modify by 2023-02-21
        if(Tools.getInteger(copyConfigPlanSaveDto.getWxworkGroupConfigPlanFlag()).intValue() == 1){
            //查询运营商所有的个人微信-微信群-配置方案
            ConfigPlanQuery configPlanQuery = new ConfigPlanQuery();
            configPlanQuery.setMerId(sourceMerId);
            configPlanQuery.setDeviceType(DeviceTypeGroupEnum.WORK_WECHAT_GROUP.getKey());
            //查询目标运营商所有的微信群配置方案
            List<ConfigPlan> configPlanList_wechatGroup = configPlanMapper.findByList(configPlanQuery);
            if(configPlanList_wechatGroup != null && configPlanList_wechatGroup.size() > 0){
                for (ConfigPlan configPlan_wechatGroup : configPlanList_wechatGroup){


                    //配置方案新id
                    Long configPlanId = IdWorker.getId();

                    String configPlanName = configPlan_wechatGroup.getName() + "(复制)";
                    ConfigPlan configPlan_name = configPlanMapper.selectByNameAndMerIdAndDeviceType(configPlanName, currentMerId, DeviceTypeGroupEnum.WORK_WECHAT_GROUP.getKey());
                    if(configPlan_name != null){//名字重复，舍弃
                        continue;
                    }
                    //复制插件件信息
                    List<SysPlugin> sysPluginsList = new ArrayList<>();

                    //群功能设置
                    if(configPlan_wechatGroup.getGroupBasesetId() != null ){
                        //群功能设置
                        SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_wechatGroup.getGroupBasesetId(), configPlan_wechatGroup.getMerId());
                        if(sysPlugin != null){
                            Long id = IdWorker.getId();
                            sysPlugin.setId(id);
                            sysPlugin.setMerId(currentMerId);
                            sysPlugin.setCreateTime(now);
                            sysPlugin.setUpdateTime(now);
                            sysPluginsList.add(sysPlugin);
                            //修改配置方案的群功能设置id
                            configPlan_wechatGroup.setGroupBasesetId(id);
                        }

                        //如果群管理功能设置配置方案，默认添加群管理功能设置插件
                        if(configPlan_wechatGroup.getDeviceType().equals(DeviceTypeGroupEnum.PRIVATE_WECHAT_GROUP.getKey())){
                            String redisKey_groupmanageplan = String.format(RedisCacheKeyEnum.WXQ_MANAGEPLAN.getKey(), configPlanId);//更新旧指令配置方案
                            redisTemplate.opsForValue().set(redisKey_groupmanageplan, sysPlugin.getData());
                        }else if(configPlan_wechatGroup.getDeviceType().equals(DeviceTypeGroupEnum.WORK_WECHAT_GROUP.getKey())){//企业微信群设置
                            String redisKey_groupmanageplan = String.format(RedisCacheKeyEnum.WXQ_WORK_MANAGEPLAN.getKey(), configPlanId);//更新旧指令配置方案
                            redisTemplate.opsForValue().set(redisKey_groupmanageplan, sysPlugin.getData());
                        }else if(configPlan_wechatGroup.getDeviceType().equals(DeviceTypeGroupEnum.TIKTOK_GROUP.getKey())){//抖音群设置
                            String redisKey_groupmanageplan = String.format(RedisCacheKeyEnum.TIKTOK_MANAGEPLAN.getKey(), configPlanId);//更新旧指令配置方案
                            redisTemplate.opsForValue().set(redisKey_groupmanageplan, sysPlugin.getData());
                        }


                    }

                    //推广裂变插件id
                    if(configPlan_wechatGroup.getUserExpandFissionId()!= null ){
                        //查询推广裂变插件id
                        SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_wechatGroup.getUserExpandFissionId(), configPlan_wechatGroup.getMerId());
                        if(sysPlugin != null){
                            Long id = IdWorker.getId();
                            sysPlugin.setId(id);
                            sysPlugin.setMerId(currentMerId);
                            sysPlugin.setCreateTime(now);
                            sysPlugin.setUpdateTime(now);
                            //新加推广裂变插件id
                            //sysPluginMapper.insert(sysPlugin);
                            sysPluginsList.add(sysPlugin);
                            //修改配置方案的推广裂变插件id
                            configPlan_wechatGroup.setUserExpandFissionId(id);
                        }
                    }


                    //提现插件id
                    if(configPlan_wechatGroup.getUserTakeBalId()!= null ){
                        //查询提现插件id
                        SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_wechatGroup.getUserTakeBalId(), configPlan_wechatGroup.getMerId());
                        if(sysPlugin != null){
                            Long id = IdWorker.getId();
                            sysPlugin.setId(id);
                            sysPlugin.setMerId(currentMerId);
                            sysPlugin.setCreateTime(now);
                            sysPlugin.setUpdateTime(now);
                            //新加提现插件id
                            //sysPluginMapper.insert(sysPlugin);
                            sysPluginsList.add(sysPlugin);
                            //修改配置方案中提现插件id

                            //清空提现账房信息-每个运营商账房信息不一样
                            String data = sysPlugin.getData();
                            if(StringUtils.isNotBlank(data)){
                                try {
                                    JSONObject jsonObject = JSONObject.parseObject(data);
                                    if(jsonObject != null){
                                        jsonObject.put("zf_app_id", "");
                                        jsonObject.put("zf_app_key", "");
                                        jsonObject.put("wxpay_zf_app_id", "");
                                        jsonObject.put("wxpay_zf_app_key", "");
                                        jsonObject.put("alipay_zf_app_id", "");
                                        jsonObject.put("alipay_zf_app_key", "");
                                        //微信红包支付密码
                                        jsonObject.put("wx_redpacket_pay_password", "");
                                    }
                                    sysPlugin.setData(JSONObject.toJSONString(jsonObject));
                                }catch (Exception e){
                                    logger.error("-提现插件转成JSON对象出错--,errror={}", e.getMessage());
                                }

                            }
                            configPlan_wechatGroup.setUserTakeBalId(id);
                        }
                    }

                    //个人信息查询插件id(账户设置)
                    if(configPlan_wechatGroup.getUserInfoQueryId()!= null ){
                        //订阅消息插件id
                        SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_wechatGroup.getUserInfoQueryId(), configPlan_wechatGroup.getMerId());
                        if(sysPlugin != null){
                            Long id = IdWorker.getId();
                            sysPlugin.setId(id);
                            sysPlugin.setMerId(currentMerId);
                            sysPlugin.setCreateTime(now);
                            sysPlugin.setUpdateTime(now);
                            //新加个人信息查询插件id
                            sysPluginsList.add(sysPlugin);
                            //修改配置方案中个人信息查询插件id
                            configPlan_wechatGroup.setUserInfoQueryId(id);
                        }
                    }

                    //用户签到插件id
                    if(configPlan_wechatGroup.getUserSigninId()!= null ){
                        //用户签到插件id
                        SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_wechatGroup.getUserSigninId(), configPlan_wechatGroup.getMerId());
                        if(sysPlugin != null){
                            Long id = IdWorker.getId();
                            sysPlugin.setId(id);
                            sysPlugin.setMerId(currentMerId);
                            sysPlugin.setCreateTime(now);
                            sysPlugin.setUpdateTime(now);
                            //新加用户签到插件id
                            sysPluginsList.add(sysPlugin);
                            //修改配置方案中用户签到插件id
                            configPlan_wechatGroup.setUserSigninId(id);
                        }
                    }

                    //导购插件id
                    if(configPlan_wechatGroup.getShoppingGuideId() != null ){
                        //社群管理插件id
                        SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_wechatGroup.getShoppingGuideId(), configPlan_wechatGroup.getMerId());
                        if(sysPlugin != null){
                            Long id = IdWorker.getId();
                            sysPlugin.setId(id);
                            sysPlugin.setMerId(currentMerId);
                            sysPlugin.setCreateTime(now);
                            sysPlugin.setUpdateTime(now);
                            //新加导购插件id
                            sysPluginsList.add(sysPlugin);
                            //修改配置方案中社群管理插件id
                            configPlan_wechatGroup.setShoppingGuideId(id);
                        }
                    }

                    //其他设置插件id
                    if(configPlan_wechatGroup.getOtherId()!= null ){
                        //其他设置插件id
                        SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_wechatGroup.getOtherId(), configPlan_wechatGroup.getMerId());
                        if(sysPlugin != null){
                            Long id = IdWorker.getId();
                            sysPlugin.setId(id);
                            sysPlugin.setMerId(currentMerId);
                            sysPlugin.setCreateTime(now);
                            sysPlugin.setUpdateTime(now);
                            //新加其他设置插件id
                            sysPluginsList.add(sysPlugin);
                            //修改配置方案中其他设置插件id
                            configPlan_wechatGroup.setOtherId(id);
                        }
                    }

                    //批量插入插件
                    if(sysPluginsList != null & sysPluginsList.size() > 0){
                        sysPluginMapper.batchAdd(sysPluginsList);
                        for(SysPlugin sysPlugin : sysPluginsList){
                            //修改插件更新缓存，确保缓存和数据库一致
                            redisTemplate.opsForValue().set(String.format(RedisCacheKeyEnum.PLUGIN.getKey(), sysPlugin.getId()) , sysPlugin.getData(), 5, TimeUnit.DAYS);
                        }
                    }

                    //查出旧指令列表
                    List<RobotCmd> robotCmdList = robotCmdMapper.selectByConfigId(configPlan_wechatGroup.getMerId(), configPlan_wechatGroup.getId());
                    if(robotCmdList != null && robotCmdList.size() > 0){
                        for (RobotCmd robotCmd : robotCmdList){
                            robotCmd.setId(IdWorker.getId()); //更新旧指令ID
                            robotCmd.setMerId(currentMerId);
                            robotCmd.setConfigId(configPlanId);//更新旧指令配置方案
                            robotCmd.setCreateTime(now);
                            robotCmd.setUpdateTime(now);
                        }

                        //批量插入指令表数据库
                        robotCmdMapper.batchAdd(robotCmdList);

                        //更新新的指令缓存redis
                        String data = Tools.getStrEmpty(JSONObject.toJSONString(robotCmdList));
                        redisTemplate.opsForValue().set(String.format(RedisCacheKeyEnum.COMMAND_LIST.getKey(), configPlanId), data);

                    }

                    //添加新的配置方案
                    configPlan_wechatGroup.setId(configPlanId);
                    configPlan_wechatGroup.setName(configPlanName);
                    configPlan_wechatGroup.setMerId(currentMerId);
                    configPlan_wechatGroup.setCreateTime(now);
                    configPlan_wechatGroup.setUpdateTime(now);
                    configPlan_wechatGroup.setIsDefault(0); //不默认

                    //清除联盟设置
                    configPlan_wechatGroup.setTbAccountId(null);
                    configPlan_wechatGroup.setJdAccountId(null);
                    configPlan_wechatGroup.setPddAccountId(null);
                    configPlan_wechatGroup.setVphAccountId(null);
                    configPlan_wechatGroup.setTlbPddAccountId(null);
                    configPlan_wechatGroup.setTlbMtAccountId(null);
                    configPlan_wechatGroup.setElmAccountId(null);
                    configPlan_wechatGroup.setTlbElmAccountId(null);
                    configPlan_wechatGroup.setDyAccountId(null);

                    //更新分佣规则
                    //会员类型组
                    configPlan_wechatGroup.setUserTypeGroupId(null);
                    //淘宝分佣规则
                    configPlan_wechatGroup.setTbCommissionPlanId(null);
                    //京东分佣规则
                    configPlan_wechatGroup.setJdCommissionPlanId(null);
                    //拼多多分佣规则
                    configPlan_wechatGroup.setPddCommissionPlanId(null);
                    //唯品会分佣规则
                    configPlan_wechatGroup.setVphCommissionPlanId(null);
                    //抖音分佣规则
                    configPlan_wechatGroup.setDyCommissionPlanId(null);
                    //本地生活分佣规则
                    configPlan_wechatGroup.setLocalLiveCommissionPlanId(null);
                    configPlanMapper.insert(configPlan_wechatGroup);
                }
            }
        }

        //公众号配置配置方案
        if(Tools.getInteger(copyConfigPlanSaveDto.getWxpubConfigPlanFlag()).intValue() == 1){
            //查询运营商所有的个人微信配置方案
            ConfigPlanQuery configPlanQuery = new ConfigPlanQuery();
            configPlanQuery.setMerId(sourceMerId);
            configPlanQuery.setDeviceType(DeviceTypeEnum.WX_PUB.getKey());
            //查询目标运营商所有的配置方案
            List<ConfigPlan> configPlanList = configPlanMapper.findByList(configPlanQuery);
            if(configPlanList != null && configPlanList.size() > 0){
                for (ConfigPlan configPlan : configPlanList){

                    //配置方案新id
                    Long configPlanId = IdWorker.getId();

                    String configPlanName = configPlan.getName() + "(复制)";
                    ConfigPlan configPlan_name = configPlanMapper.selectByNameAndMerIdAndDeviceType(configPlanName, currentMerId, DeviceTypeEnum.WX_PUB.getKey());
                    if(configPlan_name != null){//名字重复，舍弃
                        continue;
                    }
                    //复制插件件信息
                    List<SysPlugin> sysPluginsList = new ArrayList<>();
                    //淘宝插件ID
                    if(configPlan.getTbId() != null ){
                        //查询淘宝插件
                        SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan.getTbId(), configPlan.getMerId());
                        if(sysPlugin != null){
                            Long id = IdWorker.getId();
                            sysPlugin.setId(id);
                            sysPlugin.setMerId(currentMerId);
                            sysPlugin.setCreateTime(now);
                            sysPlugin.setUpdateTime(now);
                            sysPluginsList.add(sysPlugin);

                            //清空提现账房信息-每个运营商账房信息不一样
                            String data = sysPlugin.getData();
                            if(StringUtils.isNotBlank(data)){
                                try {
                                    JSONObject jsonObject = JSONObject.parseObject(data);
                                    if(jsonObject != null){
                                        jsonObject.put("amoy_gold_app_key", "");
                                        jsonObject.put("amoy_gold_app_secret", "");
                                    }
                                    sysPlugin.setData(JSONObject.toJSONString(jsonObject));
                                }catch (Exception e){
                                    logger.error("-提现插件转成JSON对象出错--,errror={}", e.getMessage());
                                }

                            }

                            //修改配置方案的淘宝插件
                            configPlan.setTbId(id);
                        }
                    }

                    //京东插件id
                    if(configPlan.getJdId()!= null ){
                        //查询京东插件id
                        SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan.getJdId(), configPlan.getMerId());
                        if(sysPlugin != null){
                            Long id = IdWorker.getId();
                            sysPlugin.setId(id);
                            sysPlugin.setMerId(currentMerId);
                            sysPlugin.setCreateTime(now);
                            sysPlugin.setUpdateTime(now);
                            //新加京东插件id
                            sysPluginsList.add(sysPlugin);
                            //修改配置方案的京东插件id
                            configPlan.setJdId(id);
                        }
                    }

                    //拼多多插件id
                    if(configPlan.getPddId()!= null ){
                        //查询拼多多插件id
                        SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan.getPddId(), configPlan.getMerId());
                        if(sysPlugin != null){
                            Long id = IdWorker.getId();
                            sysPlugin.setId(id);
                            sysPlugin.setMerId(currentMerId);
                            sysPlugin.setCreateTime(now);
                            sysPlugin.setUpdateTime(now);
                            //新加拼多多插件id
                            sysPluginsList.add(sysPlugin);
                            //修改配置方案的拼多多插件id
                            configPlan.setPddId(id);
                        }
                    }

                    //唯品会插件id
                    if(configPlan.getVphId()!= null ){
                        //查询唯品会插件id
                        SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan.getVphId(), configPlan.getMerId());
                        if(sysPlugin != null){
                            Long id = IdWorker.getId();
                            sysPlugin.setId(id);
                            sysPlugin.setMerId(currentMerId);
                            sysPlugin.setCreateTime(now);
                            sysPlugin.setUpdateTime(now);
                            //新加唯品会插件id
                            sysPluginsList.add(sysPlugin);
                            //修改配置方案的唯品会插件id
                            configPlan.setVphId(id);
                        }
                    }

                    //抖音插件id
                    if(configPlan.getDyId()!= null ){
                        //查询唯品会插件id
                        SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan.getDyId(), configPlan.getMerId());
                        if(sysPlugin != null){
                            Long id = IdWorker.getId();
                            sysPlugin.setId(id);
                            sysPlugin.setMerId(currentMerId);
                            sysPlugin.setCreateTime(now);
                            sysPlugin.setUpdateTime(now);
                            //新加唯品会插件id
                            sysPluginsList.add(sysPlugin);
                            //修改配置方案的唯品会插件id
                            configPlan.setDyId(id);
                        }
                    }

                    //快手插件id
                    if(configPlan.getKsId()!= null ){
                        //查询唯品会插件id
                        SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan.getKsId(), configPlan.getMerId());
                        if(sysPlugin != null){
                            Long id = IdWorker.getId();
                            sysPlugin.setId(id);
                            sysPlugin.setMerId(currentMerId);
                            sysPlugin.setCreateTime(now);
                            sysPlugin.setUpdateTime(now);
                            //新加快手插件id
                            sysPluginsList.add(sysPlugin);
                            //修改配置方案的快手插件id
                            configPlan.setKsId(id);
                        }
                    }

                    //本地生活插件id
                    if(configPlan.getLocalLiveId()!= null ){
                        //查询本地生活插件id
                        SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan.getLocalLiveId(), configPlan.getMerId());
                        if(sysPlugin != null){
                            Long id = IdWorker.getId();
                            sysPlugin.setId(id);
                            sysPlugin.setMerId(currentMerId);
                            sysPlugin.setCreateTime(now);
                            sysPlugin.setUpdateTime(now);
                            //新加本地生活插件id
                            sysPluginsList.add(sysPlugin);
                            //修改配置方案的本地生活插件id
                            configPlan.setLocalLiveId(id);
                        }
                    }

                    //微信好友添加插件id
                    if(configPlan.getWxUserAddId()!= null ){
                        //查询微信好友添加插件id
                        SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan.getWxUserAddId(), configPlan.getMerId());
                        if(sysPlugin != null){
                            Long id = IdWorker.getId();
                            sysPlugin.setId(id);
                            sysPlugin.setMerId(currentMerId);
                            sysPlugin.setCreateTime(now);
                            sysPlugin.setUpdateTime(now);
                            //新加微信好友添加插件id
                            //sysPluginMapper.insert(sysPlugin);
                            sysPluginsList.add(sysPlugin);
                            //修改配置方案的微信好友添加插件id
                            configPlan.setWxUserAddId(id);
                        }
                    }

                    //拉群规则
                    if(configPlan.getPullWxgroupRuleId() != null ){
                        //查询微信好友添加插件id
                        SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan.getPullWxgroupRuleId(), configPlan.getMerId());
                        if(sysPlugin != null){
                            Long id = IdWorker.getId();
                            sysPlugin.setId(id);
                            sysPlugin.setMerId(currentMerId);
                            sysPlugin.setCreateTime(now);
                            sysPlugin.setUpdateTime(now);
                            //新加微信好友添加插件id
                            //sysPluginMapper.insert(sysPlugin);
                            sysPluginsList.add(sysPlugin);
                            //修改配置方案的微信好友添加插件id
                            configPlan.setPullWxgroupRuleId(id);
                        }
                    }

                    //推广裂变插件id
                    if(configPlan.getUserExpandFissionId()!= null ){
                        //查询推广裂变插件id
                        SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan.getUserExpandFissionId(), configPlan.getMerId());
                        if(sysPlugin != null){
                            Long id = IdWorker.getId();
                            sysPlugin.setId(id);
                            sysPlugin.setMerId(currentMerId);
                            sysPlugin.setCreateTime(now);
                            sysPlugin.setUpdateTime(now);
                            //新加推广裂变插件id
                            //sysPluginMapper.insert(sysPlugin);
                            sysPluginsList.add(sysPlugin);
                            //修改配置方案的推广裂变插件id
                            configPlan.setUserExpandFissionId(id);
                        }
                    }

                    //提现插件id
                    if(configPlan.getUserTakeBalId()!= null ){
                        //查询提现插件id
                        SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan.getUserTakeBalId(), configPlan.getMerId());
                        if(sysPlugin != null){
                            Long id = IdWorker.getId();
                            sysPlugin.setId(id);
                            sysPlugin.setMerId(currentMerId);
                            sysPlugin.setCreateTime(now);
                            sysPlugin.setUpdateTime(now);
                            //新加提现插件id
                            //sysPluginMapper.insert(sysPlugin);
                            sysPluginsList.add(sysPlugin);
                            //修改配置方案中提现插件id

                            //清空提现账房信息-每个运营商账房信息不一样
                            String data = sysPlugin.getData();
                            if(StringUtils.isNotBlank(data)){
                                try {
                                    JSONObject jsonObject = JSONObject.parseObject(data);
                                    if(jsonObject != null){
                                        jsonObject.put("zf_app_id", "");
                                        jsonObject.put("zf_app_key", "");
                                        jsonObject.put("wxpay_zf_app_id", "");
                                        jsonObject.put("wxpay_zf_app_key", "");
                                        jsonObject.put("alipay_zf_app_id", "");
                                        jsonObject.put("alipay_zf_app_key", "");
                                        //微信红包支付密码
                                        jsonObject.put("wx_redpacket_pay_password", "");
                                    }
                                    sysPlugin.setData(JSONObject.toJSONString(jsonObject));
                                }catch (Exception e){
                                    logger.error("-提现插件转成JSON对象出错--,errror={}", e.getMessage());
                                }

                            }
                            configPlan.setUserTakeBalId(id);
                        }
                    }

                    //好友引导插件id
                    if(configPlan.getUserGuideId()!= null ){
                        //好友引导插件id
                        SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan.getUserGuideId(), configPlan.getMerId());
                        if(sysPlugin != null){
                            Long id = IdWorker.getId();
                            sysPlugin.setId(id);
                            sysPlugin.setMerId(currentMerId);
                            sysPlugin.setCreateTime(now);
                            sysPlugin.setUpdateTime(now);
                            //新加好友引导插件id
                            //sysPluginMapper.insert(sysPlugin);
                            sysPluginsList.add(sysPlugin);
                            //修改配置方案中好友引导插件id
                            configPlan.setUserGuideId(id);
                        }
                    }

                    //自动备注插件id
                    if(configPlan.getUserAutoRemarkId()!= null ){
                        //自动备注插件id
                        SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan.getUserAutoRemarkId(), configPlan.getMerId());
                        if(sysPlugin != null){
                            Long id = IdWorker.getId();
                            sysPlugin.setId(id);
                            sysPlugin.setMerId(currentMerId);
                            sysPlugin.setCreateTime(now);
                            sysPlugin.setUpdateTime(now);
                            //新加自动备注插件id
                            sysPluginsList.add(sysPlugin);
                            //修改配置方案中自动备注插件id
                            configPlan.setUserAutoRemarkId(id);
                        }
                    }

                    //订阅消息插件id
                    if(configPlan.getSubscribeMsgId()!= null ){
                        //订阅消息插件id
                        SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan.getSubscribeMsgId(), configPlan.getMerId());
                        if(sysPlugin != null){
                            Long id = IdWorker.getId();
                            sysPlugin.setId(id);
                            sysPlugin.setMerId(currentMerId);
                            sysPlugin.setCreateTime(now);
                            sysPlugin.setUpdateTime(now);
                            //新加订阅消息插件id
                            sysPluginsList.add(sysPlugin);
                            //修改配置方案中订阅消息插件id
                            configPlan.setSubscribeMsgId(id);
                        }
                    }

                    //个人信息查询插件id
                    if(configPlan.getUserInfoQueryId()!= null ){
                        //订阅消息插件id
                        SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan.getUserInfoQueryId(), configPlan.getMerId());
                        if(sysPlugin != null){
                            Long id = IdWorker.getId();
                            sysPlugin.setId(id);
                            sysPlugin.setMerId(currentMerId);
                            sysPlugin.setCreateTime(now);
                            sysPlugin.setUpdateTime(now);
                            //新加个人信息查询插件id
                            sysPluginsList.add(sysPlugin);
                            //修改配置方案中个人信息查询插件id
                            configPlan.setUserInfoQueryId(id);
                        }
                    }

                    //用户签到插件id
                    if(configPlan.getUserSigninId()!= null ){
                        //用户签到插件id
                        SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan.getUserSigninId(), configPlan.getMerId());
                        if(sysPlugin != null){
                            Long id = IdWorker.getId();
                            sysPlugin.setId(id);
                            sysPlugin.setMerId(currentMerId);
                            sysPlugin.setCreateTime(now);
                            sysPlugin.setUpdateTime(now);
                            //新加用户签到插件id
                            sysPluginsList.add(sysPlugin);
                            //修改配置方案中用户签到插件id
                            configPlan.setUserSigninId(id);
                        }
                    }

                    //社群管理插件id
                    if(configPlan.getCommunityId()!= null ){
                        //社群管理插件id
                        SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan.getCommunityId(), configPlan.getMerId());
                        if(sysPlugin != null){
                            Long id = IdWorker.getId();
                            sysPlugin.setId(id);
                            sysPlugin.setMerId(currentMerId);
                            sysPlugin.setCreateTime(now);
                            sysPlugin.setUpdateTime(now);
                            //新加社群管理插件id
                            sysPluginsList.add(sysPlugin);
                            //修改配置方案中社群管理插件id
                            configPlan.setCommunityId(id);
                        }
                    }

                    //导购插件id
                    if(configPlan.getShoppingGuideId() != null ){
                        //社群管理插件id
                        SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan.getShoppingGuideId(), configPlan.getMerId());
                        if(sysPlugin != null){
                            Long id = IdWorker.getId();
                            sysPlugin.setId(id);
                            sysPlugin.setMerId(currentMerId);
                            sysPlugin.setCreateTime(now);
                            sysPlugin.setUpdateTime(now);
                            //新加社群管理插件id
                            sysPluginsList.add(sysPlugin);
                            //修改配置方案中社群管理插件id
                            configPlan.setShoppingGuideId(id);
                        }
                    }

                    //其他设置插件id
                    if(configPlan.getOtherId()!= null ){
                        //其他设置插件id
                        SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan.getOtherId(), configPlan.getMerId());
                        if(sysPlugin != null){
                            Long id = IdWorker.getId();
                            sysPlugin.setId(id);
                            sysPlugin.setMerId(currentMerId);
                            sysPlugin.setCreateTime(now);
                            sysPlugin.setUpdateTime(now);
                            //新加其他设置插件id
                            sysPluginsList.add(sysPlugin);
                            //修改配置方案中其他设置插件id
                            configPlan.setOtherId(id);
                        }
                    }

                    //互通设置id
                    if(configPlan.getWxpubSettingId()!= null ){
                        //其他设置插件id
                        SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan.getWxpubSettingId(), configPlan.getMerId());
                        if(sysPlugin != null){
                            Long id = IdWorker.getId();
                            sysPlugin.setId(id);
                            sysPlugin.setMerId(currentMerId);
                            sysPlugin.setCreateTime(now);
                            sysPlugin.setUpdateTime(now);
                            //新加其他设置插件id
                            sysPluginsList.add(sysPlugin);
                            //修改配置方案中互通设置插件id
                            configPlan.setWxpubSettingId(id);
                        }
                    }

                    //批量插入插件
                    if(sysPluginsList != null & sysPluginsList.size() > 0){
                        sysPluginMapper.batchAdd(sysPluginsList);
                        for(SysPlugin sysPlugin : sysPluginsList){
                            //修改插件更新缓存，确保缓存和数据库一致
                            redisTemplate.opsForValue().set(String.format(RedisCacheKeyEnum.PLUGIN.getKey(), sysPlugin.getId()) , sysPlugin.getData(), 5, TimeUnit.DAYS);
                        }
                    }

                    //查出旧指令列表
                    List<RobotCmd> robotCmdList = robotCmdMapper.selectByConfigId(configPlan.getMerId(), configPlan.getId());
                    if(robotCmdList != null && robotCmdList.size() > 0){
                        for (RobotCmd robotCmd : robotCmdList){
                            robotCmd.setId(IdWorker.getId()); //更新旧指令ID
                            robotCmd.setMerId(currentMerId);
                            robotCmd.setConfigId(configPlanId);//更新旧指令配置方案
                            robotCmd.setCreateTime(now);
                            robotCmd.setUpdateTime(now);
                        }

                        //批量插入指令表数据库
                        robotCmdMapper.batchAdd(robotCmdList);

                        //更新新的指令缓存redis
                        String data = Tools.getStrEmpty(JSONObject.toJSONString(robotCmdList));
                        redisTemplate.opsForValue().set(String.format(RedisCacheKeyEnum.COMMAND_LIST.getKey(), configPlanId), data);

                    }

                    //添加新的配置方案
                    configPlan.setId(configPlanId);
                    configPlan.setName(configPlanName);
                    configPlan.setMerId(currentMerId);
                    configPlan.setCreateTime(now);
                    configPlan.setUpdateTime(now);
                    configPlan.setIsDefault(0); //不默认

                    //清除联盟设置
                    configPlan.setTbAccountId(null);
                    configPlan.setJdAccountId(null);
                    configPlan.setPddAccountId(null);
                    configPlan.setVphAccountId(null);
                    configPlan.setTlbPddAccountId(null);
                    configPlan.setTlbMtAccountId(null);
                    configPlan.setElmAccountId(null);
                    configPlan.setTlbElmAccountId(null);
                    configPlan.setDyAccountId(null);

                    //更新分佣规则
                    //会员类型组
                    configPlan.setUserTypeGroupId(null);
                    //淘宝分佣规则
                    configPlan.setTbCommissionPlanId(null);
                    //京东分佣规则
                    configPlan.setJdCommissionPlanId(null);
                    //拼多多分佣规则
                    configPlan.setPddCommissionPlanId(null);
                    //唯品会分佣规则
                    configPlan.setVphCommissionPlanId(null);
                    //抖音分佣规则
                    configPlan.setDyCommissionPlanId(null);
                    //本地生活分佣规则
                    configPlan.setLocalLiveCommissionPlanId(null);
                    configPlanMapper.insert(configPlan);
                }
            }
        }


        //奖励方案
        if(Tools.getInteger(copyConfigPlanSaveDto.getRegRewardRuleFlag()).intValue() == 1){
            //查询运营商所有的奖励方案
            RegRewardRuleQuery regRewardRuleQuery = new RegRewardRuleQuery();
            regRewardRuleQuery.setMerId(sourceMerId);
            //查询目标运营商所有的配置方案
            List<RegRewardRule> regRewardRuleList = regRewardRuleMapper.findByList(regRewardRuleQuery);
            if(regRewardRuleList != null && regRewardRuleList.size() > 0) {
                for (RegRewardRule regRewardRule : regRewardRuleList) {
                    String regRewardRuleName = regRewardRule.getName() + "(复制)";
                    RegRewardRule regRewardRule_name = regRewardRuleMapper.selectByNameAndMerId(regRewardRuleName, currentMerId);
                    if (regRewardRule_name != null) {//名字重复，舍弃
                        continue;
                    }
                    //解析JSON.得到邀请奖励id
                    JSONObject dataObject = JSONObject.parseObject(regRewardRule.getData());
                    if(dataObject != null){
                        //邀请奖励id
                        Long inviteRewardId = dataObject.getLong("invite_reward_id");
                        if(inviteRewardId != null){
                            InviteRewardRuleQuery inviteRewardRuleQuery = new InviteRewardRuleQuery();
                            inviteRewardRuleQuery.setMerId(currentMerId);
                            List<InviteRewardRule> inviteRewardRuleList = inviteRewardRuleMapper.findByList(inviteRewardRuleQuery);
                            if(inviteRewardRuleList != null && inviteRewardRuleList.size() > 0){
                                //获取当前运营商的邀请额外奖励列表中的第一个
                                InviteRewardRule inviteRewardRule = inviteRewardRuleList.get(0);
                                if(inviteRewardRule != null){
                                    //修改注册奖励JSON的邀请奖励id
                                    dataObject.put("invite_reward_id", inviteRewardRule.getId());
                                    regRewardRule.setData(dataObject.toJSONString());
                                }
                            }
                        }
                    }
                    regRewardRule.setId(IdWorker.getId());
                    regRewardRule.setName(regRewardRuleName);
                    regRewardRule.setMerId(currentMerId);
                    regRewardRule.setCreateTime(now);
                    regRewardRule.setUpdateTime(now);
                    regRewardRuleMapper.insert(regRewardRule);
                }
            }
        }

        //定向规则
        if(Tools.getInteger(copyConfigPlanSaveDto.getPriRewardRuleFlag()).intValue() == 1){
            //查询运营商所有的定向比例
            PriRewardRuleQuery priRewardRuleQuery = new PriRewardRuleQuery();
            priRewardRuleQuery.setMerId(sourceMerId);
            //查询目标运营商所有的配置方案
            List<PriRewardRule> priRewardRuleList = priRewardRuleMapper.findByList(priRewardRuleQuery);
            if(priRewardRuleList != null && priRewardRuleList.size() > 0) {
                for (PriRewardRule priRewardRule : priRewardRuleList) {
                    String priRewardRuleName = priRewardRule.getName() + "(复制)";
                    priRewardRule.setId(IdWorker.getId());
                    priRewardRule.setName(priRewardRuleName);
                    priRewardRule.setMerId(currentMerId);
                    priRewardRule.setCreateTime(now);
                    priRewardRule.setUpdateTime(now);
                    priRewardRuleMapper.insert(priRewardRule);
                }
            }
        }

        //阶梯规则
        if(Tools.getInteger(copyConfigPlanSaveDto.getLadderRatioFlag()).intValue() == 1) {
            //查询运营商所有的阶梯规则组
            LadderRatioGroupQuery ladderRatioGroupQuery = new LadderRatioGroupQuery();
            ladderRatioGroupQuery.setMerId(sourceMerId);
            List<LadderRatioGroup> ladderRatioGroupList = ladderRatioGroupMapper.findByList(ladderRatioGroupQuery);
            if(ladderRatioGroupList != null && ladderRatioGroupList.size() > 0){
                for (LadderRatioGroup ladderRatioGroup : ladderRatioGroupList) {
                    String ladderRatioGroupName = ladderRatioGroup.getName() + "(复制)";

                    //验证是否重名
                    LadderRatioGroup ladderRatioGroup_name = ladderRatioGroupMapper.selectByName(currentMerId, ladderRatioGroupName);
                    if(ladderRatioGroup_name != null){
                        continue;
                    }

                    Long groupId_old = ladderRatioGroup.getId(); //旧分组id
                    Long groupId_new = IdWorker.getId();
                    ladderRatioGroup.setId(groupId_new);
                    ladderRatioGroup.setName(ladderRatioGroupName);
                    ladderRatioGroup.setMerId(currentMerId);
                    ladderRatioGroup.setCreateTime(now);
                    ladderRatioGroup.setUpdateTime(now);
                    ladderRatioGroupMapper.insert(ladderRatioGroup);

                    //查询该阶梯分组下的所有的阶梯规则
                    LadderRatioQuery ladderRatioQuery = new LadderRatioQuery();
                    ladderRatioQuery.setMerId(sourceMerId);
                    ladderRatioQuery.setGroupId(groupId_old);
                    List<LadderRatio> ladderRatioList = ladderRatioMapper.findByList(ladderRatioQuery);
                    if(ladderRatioList != null && ladderRatioList.size() > 0) {
                        for (LadderRatio ladderRatio : ladderRatioList){
                            //添加阶梯规则
                            ladderRatio.setId(IdWorker.getId());
                            ladderRatio.setMerId(currentMerId);
                            ladderRatio.setGroupId(groupId_new);
                            ladderRatio.setCreateTime(now);
                            ladderRatio.setUpdateTime(now);
                            ladderRatioMapper.insert(ladderRatio);
                        }
                    }
                }
            }
        }

        //会员类型
        if(Tools.getInteger(copyConfigPlanSaveDto.getUserTypeFlag()).intValue() == 1) {
            //先复制会员类型组
            UserTypeGroupQuery userTypeGroupQuery = new UserTypeGroupQuery();
            userTypeGroupQuery.setMerId(sourceMerId);
            List<UserTypeGroup> userTypeGroupList = userTypeGroupMapper.findByList(userTypeGroupQuery);
            if (userTypeGroupList != null && userTypeGroupList.size() > 0) {
                for (UserTypeGroup userTypeGroup : userTypeGroupList) {
                    Long userTypeGroupId_old = userTypeGroup.getId();
                    Long userTypeGroupId_new = IdWorker.getId();
                    String userTypeGroupName = userTypeGroup.getName() + "(复制)";
                    //验证是否重名
                    UserTypeGroup userTypeGroup_name = userTypeGroupMapper.selectByName(currentMerId, userTypeGroupName);
                    if(userTypeGroup_name != null){
                        continue;
                    }
                    userTypeGroup.setId(userTypeGroupId_new);
                    userTypeGroup.setName(userTypeGroupName);
                    userTypeGroup.setMerId(currentMerId);
                    userTypeGroup.setCreateTime(now);
                    userTypeGroup.setUpdateTime(now);
                    userTypeGroupMapper.insert(userTypeGroup);

                    //在查询会员类型
                    UserTypeQuery userTypeQuery = new UserTypeQuery();
                    userTypeQuery.setMerId(sourceMerId);
                    userTypeQuery.setGroupId(userTypeGroupId_old);
                    List<UserType> userTypeList = userTypeMapper.findByList(userTypeQuery);
                    if(userTypeList != null && userTypeList.size() > 0){
                        for (UserType userType : userTypeList){
                            String userTypeName = userType.getName() + "(复制)";
                            userType.setId(IdWorker.getId());
                            userType.setName(userTypeName);
                            userType.setGroupId(userTypeGroupId_new);
                            userType.setMerId(currentMerId);
                            userType.setCreateTime(now);
                            userType.setUpdateTime(now);
                            userTypeMapper.insert(userType);
                        }
                    }
                }
            }
        }

        //红包模板
        if(Tools.getInteger(copyConfigPlanSaveDto.getRedPacketTemplateFlag()).intValue() == 1) {
            RedPacketTemplateQuery redPacketTemplateQuery = new RedPacketTemplateQuery();
            redPacketTemplateQuery.setMerId(sourceMerId);
            List<RedPacketTemplate> redPacketTemplateList = redPacketTemplateMapper.findByList(redPacketTemplateQuery);
            if (redPacketTemplateList != null && redPacketTemplateList.size() > 0) {
                for (RedPacketTemplate redPacketTemplate : redPacketTemplateList) {
                    String redPacketTemplateName = redPacketTemplate.getName()+ "(复制)";
                    //判断红包模板是否重复
                    RedPacketTemplate redPacketTemplate_old = redPacketTemplateMapper.selectByName(currentMerId, redPacketTemplateName);
                    if(redPacketTemplate_old != null){//名字重复，舍弃
                        continue;
                    }

                    redPacketTemplate.setId(IdWorker.getId());
                    redPacketTemplate.setName(redPacketTemplateName);
                    redPacketTemplate.setMerId(currentMerId);
                    redPacketTemplate.setCreateTime(now);
                    redPacketTemplate.setUpdateTime(now);
                    redPacketTemplateMapper.insert(redPacketTemplate);
                }
            }
        }

        //个微企微词库
        if(Tools.getInteger(copyConfigPlanSaveDto.getFaqReplyFlag()).intValue() == 1) {
            FaqReplyQuery faqReplyQuery = new FaqReplyQuery();
            faqReplyQuery.setMerId(sourceMerId);
            List<FaqReply> faqRepList = faqReplyMapper.findByList(faqReplyQuery);
            if (faqRepList != null && faqRepList.size() > 0) {
                for (FaqReply faqReply : faqRepList) {
                    //快捷回复语旧ID
                    Long faqReplyId_old = faqReply.getId();
                    //快捷回复新id
                    Long faqReplyId_new = IdWorker.getId();
                    faqReply.setId(faqReplyId_new);
                    faqReply.setMerId(currentMerId);
                    faqReply.setCreateTime(now);
                    faqReply.setUpdateTime(now);
                    faqReplyMapper.insert(faqReply);

                    //关键词
                    FaqKeywordQuery faqKeywordQuery = new FaqKeywordQuery();
                    faqKeywordQuery.setMerId(sourceMerId);
                    faqKeywordQuery.setReplyId(faqReplyId_old);
                    faqKeywordQuery.setDeviceUniqueId(0L);//全部机器人
                    List<FaqKeyword> faqKeywordList = faqKeywordMapper.findByList(faqKeywordQuery);
                    if(faqKeywordList != null && faqKeywordList.size() > 0){
                        for (FaqKeyword faqKeyword : faqKeywordList){
                            //未选择机器人，判断该关键字是否存在，
                            if(Tools.getLong(faqKeyword.getDeviceUniqueId()).longValue() == 0){
                                FaqKeywordQuery faqKeywordQuery_check = new FaqKeywordQuery();
                                faqKeywordQuery_check.setMerId(currentMerId);
                                faqKeywordQuery_check.setKeyword(faqKeyword.getKeyword());
                                List<FaqKeyword> faqKeywordList_check = faqKeywordMapper.findByList(faqKeywordQuery_check);
                                if(faqKeywordList_check != null && faqKeywordList_check.size() > 0){
                                   continue;
                                }
                            }
                            faqKeyword.setId(IdWorker.getId());
                            faqKeyword.setMerId(currentMerId);
                            faqKeyword.setReplyId(faqReplyId_new);
                            faqKeyword.setCreateTime(now);
                            faqKeyword.setUpdateTime(now);
                            faqKeywordMapper.insert(faqKeyword);
                        }
                    }
                }
            }
        }

        //个微企微词库
        if(Tools.getInteger(copyConfigPlanSaveDto.getKefuTalkFlag()).intValue() == 1) {
            //先添加快捷短语分类
            KefuTalkTypeQuery kefuTalkTypeQuery = new KefuTalkTypeQuery();
            kefuTalkTypeQuery.setMerId(sourceMerId);
            List<KefuTalkType> kefuTalkTypeList = kefuTalkTypeMapper.findByList(kefuTalkTypeQuery);
            if (kefuTalkTypeList != null && kefuTalkTypeList.size() > 0) {
                for (KefuTalkType kefuTalkType : kefuTalkTypeList) {
                    Long kefuTalkType_old = kefuTalkType.getId();
                    Long kefuTalkType_new = IdWorker.getId();
                    String kefuTalkTypeName = kefuTalkType.getName() + "(复制)";
                    kefuTalkType.setId(kefuTalkType_new);
                    kefuTalkType.setName(kefuTalkTypeName);
                    kefuTalkType.setMerId(currentMerId);
                    kefuTalkType.setCreateTime(now);
                    kefuTalkType.setUpdateTime(now);
                    kefuTalkTypeMapper.insert(kefuTalkType);
                    //在查询快捷短语
                    KefuTalkQuery kefuTalkQuery = new KefuTalkQuery();
                    kefuTalkQuery.setMerId(sourceMerId);
                    kefuTalkQuery.setTalkType(kefuTalkType_old);
                    List<KefuTalk> kefuTalkList = kefuTalkMapper.findByList(kefuTalkQuery);
                    if(kefuTalkList != null && kefuTalkList.size() > 0){
                        for (KefuTalk kefuTalk : kefuTalkList){
                            kefuTalk.setId(IdWorker.getId());
                            kefuTalk.setMerId(currentMerId);
                            kefuTalk.setTalkType(kefuTalkType_new);
                            kefuTalk.setCreateTime(now);
                            kefuTalk.setUpdateTime(now);
                            kefuTalkMapper.insert(kefuTalk);
                        }
                    }
                }
            }
        }
    }

    /**
     * 复制自己的配置方案
     * @author liuqi
     * @date 2021/8/13 11:38
     * @return null
     */
    public void copyPrivateConfigPlan(CopyPrivateConfigPlanDto copyPrivateConfigPlanDto){
        //运营商id
        Long merId = copyPrivateConfigPlanDto.getMerId();
        //配置方案id
        Long configId = copyPrivateConfigPlanDto.getConfigId();
        //复制到的设备类型(private_wechat:个人微信；work_wechat：企业微信；wx_pub：公众号)
        String deviceType = copyPrivateConfigPlanDto.getDeviceType();

        if(merId == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "系统参数错误");
        }

        if(configId == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请选择配置方案");
        }

        if(StringUtils.isBlank(deviceType)){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请选择复制之后的设备类型");
        }

        ConfigPlan configPlan_default = configPlanMapper.selectById(configId, merId);

        if(configPlan_default == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "选择的配置方案不存在或已删除");
        }

        int now = DateUtil.getNowTime();

        //复制插件件信息
        List<SysPlugin> sysPluginsList = new ArrayList<>();
        Long configPlanId = IdWorker.getId();

        //淘宝插件ID
        if (configPlan_default.getTbId() != null) {
            //查询淘宝插件
            SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_default.getTbId(), configPlan_default.getMerId());
            if (sysPlugin != null) {
                Long id = IdWorker.getId();
                sysPlugin.setId(id);
                sysPlugin.setMerId(merId);
                sysPlugin.setCreateTime(now);
                sysPlugin.setUpdateTime(now);
                sysPluginsList.add(sysPlugin);
                //修改配置方案的淘宝插件
                configPlan_default.setTbId(id);
            }
        }

        //京东插件id
        if (configPlan_default.getJdId() != null) {
            //查询京东插件id
            SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_default.getJdId(), configPlan_default.getMerId());
            if (sysPlugin != null) {
                Long id = IdWorker.getId();
                sysPlugin.setId(id);
                sysPlugin.setMerId(merId);
                sysPlugin.setCreateTime(now);
                sysPlugin.setUpdateTime(now);
                //新加京东插件id
                //sysPluginMapper.insert(sysPlugin);
                sysPluginsList.add(sysPlugin);
                //修改配置方案的京东插件id
                configPlan_default.setJdId(id);
            }
        }

        //拼多多插件id
        if (configPlan_default.getPddId() != null) {
            //查询拼多多插件id
            SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_default.getPddId(), configPlan_default.getMerId());
            if (sysPlugin != null) {
                Long id = IdWorker.getId();
                sysPlugin.setId(id);
                sysPlugin.setMerId(merId);
                sysPlugin.setCreateTime(now);
                sysPlugin.setUpdateTime(now);
                //新加拼多多插件id
                //sysPluginMapper.insert(sysPlugin);
                sysPluginsList.add(sysPlugin);
                //修改配置方案的拼多多插件id
                configPlan_default.setPddId(id);
            }
        }

        //唯品会插件id
        if (configPlan_default.getVphId() != null) {
            //查询唯品会插件id
            SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_default.getVphId(), configPlan_default.getMerId());
            if (sysPlugin != null) {
                Long id = IdWorker.getId();
                sysPlugin.setId(id);
                sysPlugin.setMerId(merId);
                sysPlugin.setCreateTime(now);
                sysPlugin.setUpdateTime(now);
                //新加唯品会插件id
                //sysPluginMapper.insert(sysPlugin);
                sysPluginsList.add(sysPlugin);
                //修改配置方案的唯品会插件id
                configPlan_default.setVphId(id);
            }
        }

        //本地生活插件id
        if (configPlan_default.getLocalLiveId() != null) {
            //查询本地生活插件id
            SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_default.getLocalLiveId(), configPlan_default.getMerId());
            if (sysPlugin != null) {
                Long id = IdWorker.getId();
                sysPlugin.setId(id);
                sysPlugin.setMerId(merId);
                sysPlugin.setCreateTime(now);
                sysPlugin.setUpdateTime(now);
                //新加本地生活插件id
                //sysPluginMapper.insert(sysPlugin);
                sysPluginsList.add(sysPlugin);
                //修改配置方案的本地生活插件id
                configPlan_default.setLocalLiveId(id);

                //本地生活指令
                //robotCmdService.addRobotCmdForConfigPlan(configPlan.getMerId(), configPlan.getId(), PluginKeyEnum.LOCAL_LIVE.getValue(), sysPlugin.getData());
            }
        }

        //抖音插件id
        if (configPlan_default.getDyId() != null) {
            //查询唯品会插件id
            SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_default.getDyId(), configPlan_default.getMerId());
            if (sysPlugin != null) {
                Long id = IdWorker.getId();
                sysPlugin.setId(id);
                sysPlugin.setMerId(merId);
                sysPlugin.setCreateTime(now);
                sysPlugin.setUpdateTime(now);
                //新加唯品会插件id
                //sysPluginMapper.insert(sysPlugin);
                sysPluginsList.add(sysPlugin);
                //修改配置方案的唯品会插件id
                configPlan_default.setDyId(id);
            }
        }

        //快手插件id
        if(configPlan_default.getKsId()!= null ){
            //查询唯品会插件id
            SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_default.getKsId(), configPlan_default.getMerId());
            if(sysPlugin != null){
                Long id = IdWorker.getId();
                sysPlugin.setId(id);
                sysPlugin.setMerId(merId);
                sysPlugin.setCreateTime(now);
                sysPlugin.setUpdateTime(now);
                //新加快手插件id
                sysPluginsList.add(sysPlugin);
                //修改配置方案的快手插件id
                configPlan_default.setKsId(id);
            }
        }

        //更新分佣规则
        //会员类型组
        configPlan_default.setUserTypeGroupId(null);
        //淘宝分佣规则
        configPlan_default.setTbCommissionPlanId(null);
        //京东分佣规则
        configPlan_default.setJdCommissionPlanId(null);
        //拼多多分佣规则
        configPlan_default.setPddCommissionPlanId(null);
        //唯品会分佣规则
        configPlan_default.setVphCommissionPlanId(null);
        //抖音分佣规则
        configPlan_default.setDyCommissionPlanId(null);
        //本地生活分佣规则
        configPlan_default.setLocalLiveCommissionPlanId(null);

        //拉群规则
        if (configPlan_default.getPullWxgroupRuleId() != null) {
            //查询微信好友添加插件id
            SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_default.getPullWxgroupRuleId(), configPlan_default.getMerId());
            if (sysPlugin != null) {
                Long id = IdWorker.getId();
                sysPlugin.setId(id);
                sysPlugin.setMerId(merId);
                sysPlugin.setCreateTime(now);
                sysPlugin.setUpdateTime(now);
                //新加微信好友添加插件id
                //sysPluginMapper.insert(sysPlugin);
                sysPluginsList.add(sysPlugin);
                //修改配置方案的微信好友添加插件id
                configPlan_default.setPullWxgroupRuleId(id);
            }
        }

        //微信好友添加插件id
        if (configPlan_default.getWxUserAddId() != null) {
            //查询微信好友添加插件id
            SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_default.getWxUserAddId(), configPlan_default.getMerId());
            if (sysPlugin != null) {
                Long id = IdWorker.getId();
                sysPlugin.setId(id);
                sysPlugin.setMerId(merId);
                sysPlugin.setCreateTime(now);
                sysPlugin.setUpdateTime(now);
                //新加微信好友添加插件id
                //sysPluginMapper.insert(sysPlugin);
                sysPluginsList.add(sysPlugin);
                //修改配置方案的微信好友添加插件id
                configPlan_default.setWxUserAddId(id);
            }
        }

        //推广裂变插件id
        if (configPlan_default.getUserExpandFissionId() != null) {
            //查询推广裂变插件id
            SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_default.getUserExpandFissionId(), configPlan_default.getMerId());
            if (sysPlugin != null) {
                Long id = IdWorker.getId();
                sysPlugin.setId(id);
                sysPlugin.setMerId(merId);
                sysPlugin.setCreateTime(now);
                sysPlugin.setUpdateTime(now);
                //新加推广裂变插件id
                //sysPluginMapper.insert(sysPlugin);
                sysPluginsList.add(sysPlugin);
                //修改配置方案的推广裂变插件id
                configPlan_default.setUserExpandFissionId(id);
            }
        }
        //提现插件id
        if (configPlan_default.getUserTakeBalId() != null) {
            //查询提现插件id
            SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_default.getUserTakeBalId(), configPlan_default.getMerId());
            if (sysPlugin != null) {
                Long id = IdWorker.getId();
                sysPlugin.setId(id);
                sysPlugin.setMerId(merId);
                sysPlugin.setCreateTime(now);
                sysPlugin.setUpdateTime(now);
                //新加提现插件id
                //sysPluginMapper.insert(sysPlugin);
                sysPluginsList.add(sysPlugin);
                //修改配置方案中提现插件id
                configPlan_default.setUserTakeBalId(id);

                //保存提现指令
                //robotCmdService.addRobotCmdForConfigPlan(configPlan.getMerId(), configPlan.getId(), PluginKeyEnum.USER_TAKE_BAL.getValue(), sysPlugin.getData());
            }
        }
        //好友引导插件id
        if (configPlan_default.getUserGuideId() != null) {
            //好友引导插件id
            SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_default.getUserGuideId(), configPlan_default.getMerId());
            if (sysPlugin != null) {
                Long id = IdWorker.getId();
                sysPlugin.setId(id);
                sysPlugin.setMerId(merId);
                sysPlugin.setCreateTime(now);
                sysPlugin.setUpdateTime(now);
                //新加好友引导插件id
                //sysPluginMapper.insert(sysPlugin);
                sysPluginsList.add(sysPlugin);
                //修改配置方案中好友引导插件id
                configPlan_default.setUserGuideId(id);
            }
        }
        //自动备注插件id
        if (configPlan_default.getUserAutoRemarkId() != null) {
            //自动备注插件id
            SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_default.getUserAutoRemarkId(), configPlan_default.getMerId());
            if (sysPlugin != null) {
                Long id = IdWorker.getId();
                sysPlugin.setId(id);
                sysPlugin.setMerId(merId);
                sysPlugin.setCreateTime(now);
                sysPlugin.setUpdateTime(now);
                //新加自动备注插件id
                //sysPluginMapper.insert(sysPlugin);
                sysPluginsList.add(sysPlugin);
                //修改配置方案中自动备注插件id
                configPlan_default.setUserAutoRemarkId(id);
            }
        }

        //订阅消息插件id
        if (configPlan_default.getSubscribeMsgId() != null) {
            //订阅消息插件id
            SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_default.getSubscribeMsgId(), configPlan_default.getMerId());
            if (sysPlugin != null) {
                Long id = IdWorker.getId();
                sysPlugin.setId(id);
                sysPlugin.setMerId(merId);
                sysPlugin.setCreateTime(now);
                sysPlugin.setUpdateTime(now);
                //新加订阅消息插件id
                //sysPluginMapper.insert(sysPlugin);
                sysPluginsList.add(sysPlugin);
                //修改配置方案中订阅消息插件id
                configPlan_default.setSubscribeMsgId(id);

                //保存订阅指令
                //robotCmdService.addRobotCmdForConfigPlan(configPlan.getMerId(), configPlan.getId(), PluginKeyEnum.SUBSCRIBE_MSG.getValue(), sysPlugin.getData());
            }
        }

        //个人信息查询插件id
        if (configPlan_default.getUserInfoQueryId() != null) {
            //订阅消息插件id
            SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_default.getUserInfoQueryId(), configPlan_default.getMerId());
            if (sysPlugin != null) {
                Long id = IdWorker.getId();
                sysPlugin.setId(id);
                sysPlugin.setMerId(merId);
                sysPlugin.setCreateTime(now);
                sysPlugin.setUpdateTime(now);
                //新加个人信息查询插件id
                //sysPluginMapper.insert(sysPlugin);
                sysPluginsList.add(sysPlugin);
                //修改配置方案中个人信息查询插件id
                configPlan_default.setUserInfoQueryId(id);

                //保存个人信息指令
                //robotCmdService.addRobotCmdForConfigPlan(configPlan.getMerId(), configPlan.getId(), PluginKeyEnum.USER_INFO_QUERY.getValue(), sysPlugin.getData());
            }
        }

        //用户签到插件id
        if (configPlan_default.getUserSigninId() != null) {
            //用户签到插件id
            SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_default.getUserSigninId(), configPlan_default.getMerId());
            if (sysPlugin != null) {
                Long id = IdWorker.getId();
                sysPlugin.setId(id);
                sysPlugin.setMerId(merId);
                sysPlugin.setCreateTime(now);
                sysPlugin.setUpdateTime(now);
                //新加用户签到插件id
                //sysPluginMapper.insert(sysPlugin);
                sysPluginsList.add(sysPlugin);
                //修改配置方案中用户签到插件id
                configPlan_default.setUserSigninId(id);

                //保存用户签到指令
                //robotCmdService.addRobotCmdForConfigPlan(configPlan.getMerId(), configPlan.getId(), PluginKeyEnum.USER_SIGNIN.getValue(), sysPlugin.getData());

            }
        }
        //社群管理插件id
        if (configPlan_default.getCommunityId() != null) {
            //社群管理插件id
            SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_default.getCommunityId(), configPlan_default.getMerId());
            if (sysPlugin != null) {
                Long id = IdWorker.getId();
                sysPlugin.setId(id);
                sysPlugin.setMerId(merId);
                sysPlugin.setCreateTime(now);
                sysPlugin.setUpdateTime(now);
                //新加社群管理插件id
                //sysPluginMapper.insert(sysPlugin);
                sysPluginsList.add(sysPlugin);
                //修改配置方案中社群管理插件id
                configPlan_default.setCommunityId(id);

                //保存社群指令
                //robotCmdService.addRobotCmdForConfigPlan(configPlan.getMerId(), configPlan.getId(), PluginKeyEnum.COMMUNITY.getValue(), sysPlugin.getData());
            }
        }

        //其他设置插件id
        if (configPlan_default.getOtherId() != null) {
            //其他设置插件id
            SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_default.getOtherId(), configPlan_default.getMerId());
            if (sysPlugin != null) {
                Long id = IdWorker.getId();
                sysPlugin.setId(id);
                sysPlugin.setMerId(merId);
                sysPlugin.setCreateTime(now);
                sysPlugin.setUpdateTime(now);
                //新加其他设置插件id
                //sysPluginMapper.insert(sysPlugin);
                sysPluginsList.add(sysPlugin);
                //修改配置方案中其他设置插件id
                configPlan_default.setOtherId(id);
            }
        }

        //导购插件id
        if (configPlan_default.getShoppingGuideId() != null) {
            //社群管理插件id
            SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_default.getShoppingGuideId(), configPlan_default.getMerId());
            if (sysPlugin != null) {
                Long id = IdWorker.getId();
                sysPlugin.setId(id);
                sysPlugin.setMerId(merId);
                sysPlugin.setCreateTime(now);
                sysPlugin.setUpdateTime(now);
                //新加社群管理插件id
                //sysPluginMapper.insert(sysPlugin);
                sysPluginsList.add(sysPlugin);
                //修改配置方案中社群管理插件id
                configPlan_default.setShoppingGuideId(id);

                //保存社群指令
                //robotCmdService.addRobotCmdForConfigPlan(configPlan.getMerId(), configPlan.getId(), PluginKeyEnum.COMMUNITY.getValue(), sysPlugin.getData());
            }
        }

        //微信公众号设置id
        if (configPlan_default.getWxpubSettingId() != null) {
            //其他设置插件id
            SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_default.getWxpubSettingId(), configPlan_default.getMerId());
            if (sysPlugin != null) {
                Long id = IdWorker.getId();
                sysPlugin.setId(id);
                sysPlugin.setMerId(merId);
                sysPlugin.setCreateTime(now);
                sysPlugin.setUpdateTime(now);
                //新加其他设置插件id
                //sysPluginMapper.insert(sysPlugin);
                sysPluginsList.add(sysPlugin);
                //修改配置方案中其他设置插件id
                configPlan_default.setWxpubSettingId(id);
            }
        }

        //微信机器人关联公众号插件id
        if (configPlan_default.getWechatUnionWxpubId() != null) {
            //其他设置插件id
            SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_default.getWechatUnionWxpubId(), configPlan_default.getMerId());
            if (sysPlugin != null) {
                Long id = IdWorker.getId();
                sysPlugin.setId(id);
                sysPlugin.setMerId(merId);
                sysPlugin.setCreateTime(now);
                sysPlugin.setUpdateTime(now);
                //新加其他设置插件id
                //sysPluginMapper.insert(sysPlugin);
                sysPluginsList.add(sysPlugin);
                //修改配置方案中其他设置插件id
                configPlan_default.setWechatUnionWxpubId(id);
            }
        }

        //群基础功能
        if(configPlan_default.getGroupBasesetId()!= null ){
            //其他设置插件id
            SysPlugin sysPlugin = sysPluginMapper.selectById(configPlan_default.getGroupBasesetId(), configPlan_default.getMerId());
            if(sysPlugin != null){
                Long id = IdWorker.getId();
                sysPlugin.setId(id);
                sysPlugin.setMerId(merId);
                sysPlugin.setCreateTime(now);
                sysPlugin.setUpdateTime(now);
                //新加其他设置插件id
                sysPluginsList.add(sysPlugin);
                //修改配置方案中其他设置插件id
                configPlan_default.setGroupBasesetId(id);
            }

            //如果群管理功能设置配置方案，默认添加群管理功能设置插件
            if(configPlan_default.getDeviceType().equals(DeviceTypeGroupEnum.PRIVATE_WECHAT_GROUP.getKey())){
                String redisKey_groupmanageplan = String.format(RedisCacheKeyEnum.WXQ_MANAGEPLAN.getKey(), configPlanId);//更新旧指令配置方案
                redisTemplate.opsForValue().set(redisKey_groupmanageplan, sysPlugin.getData());
            }else if(configPlan_default.getDeviceType().equals(DeviceTypeGroupEnum.WORK_WECHAT_GROUP.getKey())){//企业微信群设置
                String redisKey_groupmanageplan = String.format(RedisCacheKeyEnum.WXQ_WORK_MANAGEPLAN.getKey(), configPlanId);//更新旧指令配置方案
                redisTemplate.opsForValue().set(redisKey_groupmanageplan, sysPlugin.getData());
            }else if(configPlan_default.getDeviceType().equals(DeviceTypeGroupEnum.TIKTOK_GROUP.getKey())){//抖音群设置
                String redisKey_groupmanageplan = String.format(RedisCacheKeyEnum.TIKTOK_MANAGEPLAN.getKey(), configPlanId);//更新旧指令配置方案
                redisTemplate.opsForValue().set(redisKey_groupmanageplan, sysPlugin.getData());
            }
        }

        //批量插入插件
        if (sysPluginsList != null & sysPluginsList.size() > 0) {
            sysPluginMapper.batchAdd(sysPluginsList);
            for (SysPlugin sysPlugin : sysPluginsList) {
                //修改插件更新缓存，确保缓存和数据库一致
                redisTemplate.opsForValue().set(String.format(RedisCacheKeyEnum.PLUGIN.getKey(), sysPlugin.getId()), sysPlugin.getData(), 5, TimeUnit.DAYS);
            }
        }

        //查出旧指令列表
        List<RobotCmd> robotCmdList = robotCmdMapper.selectByConfigId(configPlan_default.getMerId(), configPlan_default.getId());
        if (robotCmdList != null && robotCmdList.size() > 0) {
            for (RobotCmd robotCmd : robotCmdList) {
                robotCmd.setId(IdWorker.getId()); //更新旧指令ID
                robotCmd.setMerId(merId);
                robotCmd.setConfigId(configPlanId);//更新旧指令配置方案
                robotCmd.setCreateTime(now);
                robotCmd.setUpdateTime(now);
            }

            //批量插入指令表数据库
            robotCmdMapper.batchAdd(robotCmdList);

            //更新新的指令缓存redis
            String data = Tools.getStrEmpty(JSONObject.toJSONString(robotCmdList));
            redisTemplate.opsForValue().set(String.format(RedisCacheKeyEnum.COMMAND_LIST.getKey(), configPlanId), data);

        }

        //添加新的配置方案
        configPlan_default.setId(configPlanId);
        String name_old = configPlan_default.getName();
        String name_new = configPlan_default.getName() + "(副本" + CommonUtil.random(100, 999) + ")";
        configPlan_default.setName(name_new);
        configPlan_default.setMerId(merId);
        configPlan_default.setCreateTime(now);
        configPlan_default.setUpdateTime(now);
        configPlan_default.setIsDefault(0);
        configPlan_default.setDeviceType(deviceType);

        //清除联盟设置
        configPlan_default.setTbAccountId(null);
        configPlan_default.setJdAccountId(null);
        configPlan_default.setPddAccountId(null);
        configPlan_default.setVphAccountId(null);
        configPlan_default.setTlbPddAccountId(null);
        configPlan_default.setTlbMtAccountId(null);
        configPlan_default.setElmAccountId(null);
        configPlan_default.setTlbElmAccountId(null);
        configPlan_default.setDyAccountId(null);

        configPlanMapper.insert(configPlan_default);

    }

    /**
     * 查询配置方案下拉列表
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param query
     * @return null
     */
    public List<SelectKeyVo> findConfigPlanSelectList(ConfigPlanQuery query){

        Long merId = query.getMerId();
        if(merId == null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "系统参数异常");
        }

        //新建返回配置方案下拉列表
        ArrayList<SelectKeyVo>  selectList = new ArrayList<>();
        //查询数据库中的配置方案
        ArrayList<ConfigPlan> configPlanList = configPlanMapper.findByList(query);
        if(configPlanList != null && configPlanList.size() > 0){
            for (ConfigPlan configPlan : configPlanList){
                SelectKeyVo selectListVo = new SelectKeyVo();
                selectListVo.setKey(String.valueOf(configPlan.getId()));
                selectListVo.setValue(configPlan.getName());
                selectListVo.setIsDefault(configPlan.getIsDefault());
                selectList.add(selectListVo);
            }
        }
        return selectList;
    }


}
